~mordred/+junk/hudson-jclouds

« back to all changes in this revision

Viewing changes to src/main/java/hudson/plugins/jclouds/JCloudTemplate.java

  • Committer: Monty Taylor
  • Date: 2010-05-04 00:18:19 UTC
  • Revision ID: mordred@inaugust.com-20100504001819-2proozbt4mngyn5n
Added a bunch of files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import hudson.model.Describable;
6
6
import hudson.model.Descriptor;
7
7
import hudson.model.Hudson;
 
8
import hudson.model.Label;
 
9
import hudson.model.TaskListener;
 
10
import hudson.slaves.NodeProvisioner.PlannedNode;
8
11
import hudson.util.ListBoxModel;
 
12
import java.io.IOException;
 
13
import java.io.PrintStream;
 
14
import java.util.ArrayList;
 
15
import java.util.Collection;
 
16
import java.util.List;
 
17
import java.util.Map;
 
18
import java.util.Set;
9
19
import java.util.logging.Level;
10
20
import java.util.logging.Logger;
11
21
import org.jclouds.compute.ComputeService;
12
22
import org.jclouds.compute.domain.Image;
 
23
import org.jclouds.compute.domain.NodeMetadata;
13
24
import org.jclouds.compute.domain.Template;
 
25
import org.jclouds.compute.domain.TemplateBuilder;
14
26
import org.jclouds.rest.AuthorizationException;
15
27
import org.kohsuke.stapler.DataBoundConstructor;
16
28
import org.kohsuke.stapler.QueryParameter;
19
31
 *
20
32
 * @author Monty Taylor
21
33
 */
22
 
public class SlaveTemplate implements Describable<SlaveTemplate>  {
 
34
public class JCloudTemplate implements Describable<JCloudTemplate>  {
23
35
 
24
 
    private String name;
 
36
    private String slave;
25
37
    private String description;
26
38
    private String labels;
27
39
    private String image;
28
40
 
29
41
    private String numExecutors;
 
42
 
 
43
    private transient /*almost final*/ Set<Label> labelSet;
 
44
 
30
45
    private transient JClouds parent;
31
46
 
32
47
    @DataBoundConstructor
33
 
    public SlaveTemplate(String name, String description, String labelString, String image, String numExecutors)
 
48
    public JCloudTemplate(String slave, String description, String labelString, String image, String numExecutors)
34
49
    {
35
 
        this.name = name;
 
50
        this.slave = slave;
36
51
        this.description = description;
37
52
        this.labels = Util.fixNull(labelString);
38
53
        this.image = image;
39
54
        this.numExecutors = numExecutors;
 
55
        readResolve();
40
56
    }
41
57
 
42
58
 
43
59
    private static final Logger LOGGER = Logger.getLogger(JClouds.class.getName());
 
60
 
 
61
    /**
 
62
     * Initializes data structure that we don't persist.
 
63
     */
 
64
    protected final Object readResolve() {
 
65
        labelSet = Label.parse(labels);
 
66
        return this;
 
67
    }
44
68
    
45
69
    public String getDescription() {
46
70
        return description;
58
82
        this.labels = labels;
59
83
    }
60
84
 
61
 
    public String getName() {
62
 
        return name;
63
 
    }
64
 
 
65
 
    public void setName(String name) {
66
 
        this.name = name;
67
 
    }
68
 
 
69
 
    public String getNumExecutors() {
70
 
        return numExecutors;
 
85
    public String getSlave() {
 
86
        return slave;
 
87
    }
 
88
 
 
89
    public void setSlave(String slave) {
 
90
        this.slave = slave;
 
91
    }
 
92
 
 
93
 
 
94
    public int getNumExecutors() {
 
95
       
 
96
        try {
 
97
            return Integer.parseInt(numExecutors);
 
98
        } catch (NumberFormatException e) {
 
99
            /*  @TODO: Make this based on number of CPUs (see EC2) */
 
100
            return 1;
 
101
        }
71
102
    }
72
103
 
73
104
    public void setNumExecutors(String numExecutors) {
86
117
    public void setParent(JClouds parent) {
87
118
        this.parent = parent;
88
119
    }
89
 
 
 
120
    
 
121
    /**
 
122
     * Provisions a new Compute Service
 
123
     *
 
124
     * @return always non-null. This needs to be then added to {@link Hudson#addNode(Node)}.
 
125
     */
 
126
    public List<JCloudSlave> provision(TaskListener listener, int requestedWorkload) throws AuthorizationException, Throwable {
 
127
        PrintStream logger = listener.getLogger();
 
128
 
 
129
 
 
130
        try {
 
131
            logger.println("Launching " + slave);
 
132
 
 
133
            ComputeService client = getParent().connect();
 
134
            TemplateBuilder builder = client.templateBuilder();
 
135
 
 
136
            /* @TODO We should include our options here! */
 
137
            Map<String, ? extends NodeMetadata> results = client.runNodesWithTag(slave, requestedWorkload, builder.build());
 
138
 
 
139
 
 
140
            /* Instance inst = ec2.runInstances(ami, 1, 1, Collections.<String>emptyList(), userData, keyPair.getKeyName(), type).getInstances().get(0);
 
141
            return newSlave(inst); */
 
142
            return newSlaves(results);
 
143
        } catch (Descriptor.FormException e) {
 
144
            throw new AssertionError(); // we should have discovered all configuration issues upfront
 
145
        }
 
146
    }
 
147
 
 
148
    private List<JCloudSlave> newSlaves(Map<String, ? extends NodeMetadata> nodes) throws Descriptor.FormException, IOException {
 
149
 
 
150
        List<JCloudSlave> slaves = new ArrayList<JCloudSlave>(nodes.size());
 
151
        for (String n : nodes.keySet())
 
152
        {
 
153
            /* @TODO: Actually create a real slave here */
 
154
            slaves.add(new JCloudSlave(n, labels, nodes.get(n)));
 
155
        }
 
156
        return slaves;
 
157
    }
 
158
 
 
159
    /**
 
160
     * Does this contain the given label?
 
161
     *
 
162
     * @param l
 
163
     *      can be null to indicate "don't care".
 
164
     */
 
165
    public boolean containsLabel(Label l) {
 
166
        return l==null || labelSet.contains(l);
 
167
    }
90
168
 
91
169
    @Override
92
 
    public Descriptor<SlaveTemplate> getDescriptor() {
 
170
    public Descriptor<JCloudTemplate> getDescriptor() {
93
171
        return Hudson.getInstance().getDescriptor(getClass());
94
172
    }
95
173
 
96
174
    @Extension
97
 
    public static final class DescriptorImpl extends Descriptor<SlaveTemplate> {
 
175
    public static final class DescriptorImpl extends Descriptor<JCloudTemplate> {
98
176
 
99
177
        @Override
100
178
        public String getDisplayName() {
101
179
            return null;
102
180
        }
103
181
 
104
 
        public ListBoxModel doFillImageItems(@QueryParameter String provider, @QueryParameter String user,@QueryParameter String secret) {
105
 
            
 
182
 
 
183
        public ListBoxModel doFillImageItems(@QueryParameter String provider, @QueryParameter String user, @QueryParameter String secret) {
 
184
 
 
185
            LOGGER.log(Level.INFO, "Enter doFillImageItems");
106
186
            ListBoxModel m = new ListBoxModel();
107
187
            ComputeService client = null;
108
188
            try {
109
189
                client = JClouds.getComputeService(provider, user, secret);
110
190
            } catch (Throwable ex) {
111
 
                LOGGER.log(Level.INFO, "computer service problem");
 
191
                LOGGER.log(Level.INFO, "compute service problem {0}", ex.getLocalizedMessage());
112
192
                return m;
113
193
            }
114
194
            for (Image image : client.getImages().values()) {
115
 
                m.add(image.getDescription());
 
195
                m.add(image.getDescription(), image.getId());
116
196
 
117
197
                    LOGGER.log(Level.INFO, "image: {0}|{1}|{2}:{3}", new Object[]{
118
198
                                image.getArchitecture(),