Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd61b0847fcc2bb134543e028986307c3ca5644d (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2015 Red Hat.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.ui.wizards;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.linuxtools.docker.core.DockerException;
import org.eclipse.linuxtools.docker.core.IDockerConnection;
import org.eclipse.linuxtools.docker.core.IDockerConnectionInfo;
import org.eclipse.linuxtools.docker.core.IDockerImage;
import org.eclipse.linuxtools.docker.core.IDockerImageInfo;
import org.eclipse.linuxtools.internal.docker.ui.databinding.BaseDatabindingModel;

/**
 * Databinding model for the {@link ImageRunResourceVolumesVariablesPage}
 *
 * @author xcoulon
 *
 */
public class ImageRunResourceVolumesVariablesModel
		extends BaseDatabindingModel {

	public enum MountType {
		NONE, HOST_FILE_SYSTEM, CONTAINER;
	}

	/** the 'low' CPU share weight variableValue. */
	public static final int LOW = 512;

	/** the default 'medium' CPU share weight variableValue. */
	public static final int MEDIUM = 1024;

	/** the 'high' CPU share weight variableValue. */
	public static final int HIGH = 2048;

	public static final String ENABLE_RESOURCE_LIMITATIONS = "enableResourceLimitations"; //$NON-NLS-1$

	public static final String CPU_SHARE_WEIGHT = "cpuShareWeight"; //$NON-NLS-1$

	public static final String MEMORY_LIMIT = "memoryLimit"; //$NON-NLS-1$

	public static final String DATA_VOLUMES = "dataVolumes"; //$NON-NLS-1$

	public static final String SELECTED_DATA_VOLUMES = "selectedDataVolumes"; //$NON-NLS-1$

	public static final String ENVIRONMENT_VARIABLES = "environmentVariables"; //$NON-NLS-1$

	private boolean enableResourceLimitations = false;

	private final IDockerConnection connection;

	private final IDockerConnectionInfo info;

	private IDockerImageInfo imageInfo = null;

	private int memoryLimit = 512;

	private int cpuShareWeighting = 1024;

	private Set<DataVolumeModel> selectedDataVolumes = new HashSet<>();

	private WritableList dataVolumes = new WritableList();

	private WritableList environmentVariables = new WritableList();

	private IDockerImage selectedImage;

	public ImageRunResourceVolumesVariablesModel(
			final IDockerConnection connection) throws DockerException {
		this.connection = connection;
		this.info = connection.getInfo();
	}

	public IDockerConnection getConnection() {
		return connection;
	}

	/**
	 * Refreshes the list of Volumes to display in the for the given
	 * 
	 * @param selectedImage
	 */
	public void setSelectedImage(final IDockerImage selectedImage) {
		if (this.selectedImage != selectedImage) {
			this.selectedImage = selectedImage;
			final WritableList newDataVolumes = new WritableList();
			if (selectedImage != null) {
				this.imageInfo = selectedImage.getConnection()
						.getImageInfo(selectedImage.id());
				if (this.imageInfo.config() != null
						&& this.imageInfo.config().volumes() != null) {
					for (String volume : this.imageInfo.config().volumes()) {
						newDataVolumes.add(new DataVolumeModel(volume));
					}
				}
			}
			setDataVolumes(newDataVolumes);
		}
	}

	public IDockerImageInfo getSelectedImageInfo() {
		return imageInfo;
	}

	public WritableList getDataVolumes() {
		return dataVolumes;
	}

	public void setDataVolumes(final WritableList dataVolumes) {
		this.dataVolumes.clear();
		this.dataVolumes.addAll(dataVolumes);
	}

	public void removeDataVolume(final DataVolumeModel dataVolume) {
		this.dataVolumes.remove(dataVolume);
	}

	public Set<DataVolumeModel> getSelectedDataVolumes() {
		return selectedDataVolumes;
	}

	public void setSelectedDataVolumes(
			final Set<DataVolumeModel> selectedDataVolumes) {
		firePropertyChange(SELECTED_DATA_VOLUMES, this.selectedDataVolumes,
				this.selectedDataVolumes = selectedDataVolumes);
	}

	public WritableList getEnvironmentVariables() {
		return environmentVariables;
	}

	public void setEnvironmentVariables(
			final WritableList environmentVariables) {
		this.environmentVariables.clear();
		this.environmentVariables.addAll(environmentVariables);
	}

	public void removeEnvironmentVariable(
			final EnvironmentVariableModel variable) {
		this.environmentVariables.remove(variable);
	}

	/**
	 * @return the total memory of the Docker daemon, in MB
	 * @throws DockerException
	 */
	public int getTotalMemory() {
		return (int) (this.info.getTotalMemory() / 1048576);
	}

	public boolean isEnableResourceLimitations() {
		return enableResourceLimitations;
	}

	public void setEnableResourceLimitations(
			boolean enableResourceLimitations) {
		firePropertyChange(ENABLE_RESOURCE_LIMITATIONS,
				this.enableResourceLimitations,
				this.enableResourceLimitations = enableResourceLimitations);
	}

	/**
	 * The memory allocated for the container, in MB.
	 * 
	 * @return
	 */
	public int getMemoryLimit() {
		return memoryLimit;
	}

	/**
	 * The memory allocated for the container, in Bytes.
	 * 
	 * @return
	 */
	public long getMemory() {
		return memoryLimit * 1048576;
	}

	public void setMemoryLimit(final int memoryLimit) {
		firePropertyChange(MEMORY_LIMIT, this.memoryLimit,
				this.memoryLimit = memoryLimit);
	}

	public int getCpuShareWeight() {
		return cpuShareWeighting;
	}

	public void setCpuShareWeight(final int cpuShareWeighting) {
		firePropertyChange(CPU_SHARE_WEIGHT, this.cpuShareWeighting,
				this.cpuShareWeighting = cpuShareWeighting);
	}

}

Back to the top