This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathContainerSettingsVolumes.react.js
More file actions
210 lines (186 loc) · 5.88 KB
/
ContainerSettingsVolumes.react.js
File metadata and controls
210 lines (186 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import _ from 'underscore';
import React from 'react/addons';
import electron from 'electron';
const remote = electron.remote;
const dialog = remote.dialog;
import {shell} from 'electron';
import util from '../utils/Util';
import metrics from '../utils/MetricsUtil';
import containerActions from '../actions/ContainerActions';
var ContainerSettingsVolumes = React.createClass({
getInitialState: function () {
let container = this.props.container;
if (!container) {
return false;
}
let mounts = _.clone(container.Mounts);
mounts.push({
Destination: undefined,
Mode: 'rw',
Propagation: 'rpirvate',
RW: true,
Source: undefined,
Type: 'bind'
});
return {
containerName: container.Name,
mounts
}
},
handleChooseVolumeClick: function (dockerVol) {
dialog.showOpenDialog({properties: ['openDirectory', 'createDirectory']}, (filenames) => {
if (!filenames) {
return;
}
var directory = filenames[0];
if (!directory || (!util.isNative() && directory.indexOf(util.home()) === -1)) {
dialog.showMessageBox({
type: 'warning',
buttons: ['OK'],
message: 'Invalid directory - Please make sure the directory exists and you can read/write to it.'
});
return;
}
metrics.track('Choose Directory for Volume');
let mounts = _.clone(this.state.mounts);
_.each(mounts, m => {
if (m.Destination === dockerVol) {
m.Source = util.windowsToLinuxPath(directory);
m.Driver = null;
}
});
this.setState({
mounts
});
});
},
handleRemoveVolumeClick: function (dockerVol) {
metrics.track('Removed Volume Directory', {
from: 'settings'
});
let mounts = _.clone(this.state.mounts);
_.each(mounts, m => {
if (m.Destination === dockerVol) {
m.Source = null;
m.Driver = 'local';
}
});
this.setState({
mounts
});
},
handleOpenVolumeClick: function (path) {
metrics.track('Opened Volume Directory', {
from: 'settings'
});
if (util.isWindows()) {
shell.showItemInFolder(util.linuxToWindowsPath(path));
} else {
shell.showItemInFolder(path);
}
},
handleAddVolume: function () {
let mounts = _.clone(this.state.mounts);
// undefined is clearer when reading the code
mounts.push({
Destination: undefined,
Mode: 'rw',
Propagation: 'rpirvate',
RW: true,
Source: undefined,
Type: 'bind'
});
this.setState({
mounts
});
metrics.track('Adding Pending Volume')
},
handleRemoveVolume: function (index) {
let mounts = this.state.mounts.filter((val, idx) => idx !== index);
this.setState({
mounts
});
metrics.track('Removed Volume')
},
handleDestinationChanged: function (index, event) {
let mounts = _.clone(this.state.mounts);
mounts[index].Destination = event.target.value;
this.setState({
mounts
});
},
handleSaveVolumes: function() {
let mounts = this.state.mounts;
let binds = mounts.filter(m => {
// Filter out everything that is empty/null
return !(!m.Destination || !m.Source || m.Destination === '' || m.Source === '');
}).map(m => {
return m.Source + ':' + m.Destination;
});
let hostConfig = _.extend(this.props.container.HostConfig, {Binds: binds});
containerActions.update(this.props.container.Name, {Mounts: mounts, HostConfig: hostConfig});
},
render: function () {
if (!this.props.container) {
return false;
}
var homeDir = util.isWindows() ? util.windowsToLinuxPath(util.home()) : util.home();
var mounts = _.map(this.state.mounts, (m, index) => {
let source = m.Source, destination = m.Destination;
if (!m.Source || (!util.isNative() && m.Source.indexOf(homeDir) === -1) || (m.Source.indexOf('/var/lib/docker/volumes') !== -1)) {
source = (
<span className="value-right">No Folder</span>
);
} else {
let local = util.isWindows() ? util.linuxToWindowsPath(source) : source;
source = (
<a className="value-right" onClick={this.handleOpenVolumeClick.bind(this, source)}>{local.replace(process.env.HOME, '~')}</a>
);
}
let icon;
if (index === this.state.mounts.length - 1) {
icon = <a onClick={this.handleAddVolume} className="only-icon btn btn-positive small"><span
className="icon icon-add"></span></a>;
} else {
icon = <a onClick={this.handleRemoveVolume.bind(this, index)} className="only-icon btn btn-action small"><span
className="icon icon-delete"></span></a>;
}
return (
<tr>
<td>
<input type="text" className="destination line" defaultValue={destination}
onChange={this.handleDestinationChanged.bind(this, index)}></input>
</td>
<td>{source}</td>
<td>
<a className="btn btn-action small" disabled={this.props.container.State.Updating} onClick={this.handleChooseVolumeClick.bind(this, destination)}>Change</a>
</td>
<td>{icon}</td>
</tr>
);
});
return (
<div className="settings-panel">
<div className="settings-section">
<h3>Configure Volumes</h3>
<table className="table volumes">
<thead>
<tr>
<th>DOCKER FOLDER</th>
<th>LOCAL FOLDER</th>
<th></th>
</tr>
</thead>
<tbody>
{mounts}
</tbody>
</table>
<div className="settings-section">
<a className="btn btn-action" disabled={this.props.container.State.Updating} onClick={this.handleSaveVolumes}>Save</a>
</div>
</div>
</div>
);
}
});
module.exports = ContainerSettingsVolumes;