vagrant-rancher 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07fc0ab4dffdade295fb36426c3e1a7b6979763c
4
- data.tar.gz: 7f8c27bd89e8c65c707282b8a28b10e9d4f8bd4b
3
+ metadata.gz: 5cc5f9f9ce9ca77cca5003504ebf18990b5a889c
4
+ data.tar.gz: 2237ae2b0cd21006ac30159ab52feff0e275dbea
5
5
  SHA512:
6
- metadata.gz: 0b7d6c32011364096681eced5929074fa3d7dcf03e856066a7d2dc09e5c2673f4b57c92660a8f18c286cae0802c6ad5bca51e5e3ca579a2f7e6e9691a7de05e2
7
- data.tar.gz: d01d57d525a84e44a34e519090a6d6f64b26692fe3c95f2c970d24e5f75b2dfd485ad190207c8f2676c57027204c9743e1831f42f695665231bd67ceaf8abdd4
6
+ metadata.gz: 0bec5ec68204cfaf5ef8e2c50138fe6685d1130e8fcfa4a37f3a712d03d806778cae184ad0a3123f86dbf678790ead9b63deb0ee95a6817d608154c9c66351fd
7
+ data.tar.gz: e3c094d730166f34309f395cb1d39ebb5f567e21750f452defede44be10f6aed292274c07b6bce2b947ac00f5e3a5e9ece91edb4f850ef6f325a167da99216d9
data/README.md CHANGED
@@ -41,6 +41,8 @@ The `vagrant-rancher` plugin requires the hostname being set to either a DNS nam
41
41
  * `server_args` (*optional*, default: `''`): additional args to pass to the Docker run command when starting the Rancher server
42
42
  * `labels` (*optional*, default: `[]`): array of key=value pairs of labels to assign to the agent (ex. ["role=server","env=local"])
43
43
  * `deactivate` (*optional*, default: `false`): deactivate the host in Rancher to prevent it from being scheduled on
44
+ * `project` (*optional*, default: `Default`): the project to start the agent in (project will be created if it doesn't exist)
45
+ * `project_type` (*optional*, default: `cattle`): the project type (one of 'cattle', 'swarm', or 'kubernetes')
44
46
 
45
47
  ## Examples
46
48
 
@@ -50,6 +52,13 @@ See examples directory. For a quick setup of a Rancher environment running on Ra
50
52
 
51
53
  * VirtualBox
52
54
 
55
+ ## Development
56
+
57
+ ```
58
+ bundle install
59
+ bundle exec vagrant ...
60
+ ```
61
+
53
62
  ## Contributing
54
63
 
55
64
  Bug reports and pull requests are welcome on GitHub at https://github.com/nextrevision/vagrant-rancher.
@@ -20,6 +20,8 @@ Vagrant.configure(2) do |config|
20
20
 
21
21
  node.vm.provision :rancher do |rancher|
22
22
  rancher.hostname = "192.168.33.100"
23
+ rancher.project = "vagrant"
24
+ rancher.project_type = "swarm"
23
25
  rancher.deactivate = true
24
26
  rancher.labels = ["role=server"]
25
27
  end
@@ -8,6 +8,8 @@ module VagrantPlugins
8
8
  attr_accessor :deactivate
9
9
  attr_accessor :server_args
10
10
  attr_accessor :labels
11
+ attr_accessor :project
12
+ attr_accessor :project_type
11
13
 
12
14
  def initialize
13
15
  @role = UNSET_VALUE
@@ -17,6 +19,8 @@ module VagrantPlugins
17
19
  @server_args = UNSET_VALUE
18
20
  @labels = UNSET_VALUE
19
21
  @deactivate = UNSET_VALUE
22
+ @project = UNSET_VALUE
23
+ @project_type = UNSET_VALUE
20
24
  end
21
25
 
22
26
  def finalize!
@@ -27,6 +31,8 @@ module VagrantPlugins
27
31
  @server_args = nil if @server_args == UNSET_VALUE
28
32
  @labels = nil if @labels == UNSET_VALUE
29
33
  @deactivate = false if @deactivate == UNSET_VALUE
34
+ @project = 'Default' if @project == UNSET_VALUE
35
+ @project_type = 'cattle' if @project_type == UNSET_VALUE
30
36
  end
31
37
 
32
38
  def validate(_machine)
@@ -60,6 +66,14 @@ module VagrantPlugins
60
66
  errors << ':rancher provisioner requires deactivate to be a bool'
61
67
  end
62
68
 
69
+ unless project.is_a?(String) || project.nil?
70
+ errors << ':rancher provisioner requires project to be a string'
71
+ end
72
+
73
+ unless ['cattle', 'kubernetes', 'swarm'].include? project_type || project_type.nil?
74
+ errors << ':rancher provisioner requires project_type to be one of cattle, kubernetes or swarm'
75
+ end
76
+
63
77
  { 'rancher provisioner' => errors }
64
78
  end
65
79
  end
@@ -12,15 +12,11 @@ module VagrantPlugins
12
12
  def provision
13
13
  self.check_docker
14
14
 
15
- if config.role == 'server'
16
- self.install_server
17
- self.configure_server
18
- end
15
+ self.install_server if config.role == 'server'
19
16
 
20
- if config.role == 'server' || config.role == 'agent'
21
- self.install_agent
22
- self.configure_agent
23
- end
17
+ self.configure_server
18
+ self.install_agent
19
+ self.configure_agent
24
20
  end
25
21
 
26
22
  # determines if we can reach the docker daemon
@@ -65,9 +61,40 @@ module VagrantPlugins
65
61
 
66
62
  # configures a running server with the required settings
67
63
  def configure_server
68
- # retrieve the default project id
69
- project_id = @rancher.get_project_id
70
- raise Errors::ProjectNotFound if project_id.nil?
64
+ # get the specified project
65
+ project_id = @rancher.get_project_id @config.project
66
+
67
+ # verify the project is of the correct type
68
+ unless project_id.nil?
69
+ recreate = false
70
+ project = @rancher.get_project project_id
71
+ if project['kubernetes'] and @config.project_type != 'kubernetes'
72
+ recreate = true
73
+ elsif project['swarm'] and @config.project_type != 'swarm'
74
+ recreate = true
75
+ elsif @config.project_type == 'cattle' and (project['kubernetes'] or project['swarm'])
76
+ recreate = true
77
+ end
78
+
79
+ # destroy the project and set project_id to nil when
80
+ # the project is not the specified type
81
+ if recreate
82
+ @rancher.delete_project project_id
83
+ project_id = nil
84
+ end
85
+ end
86
+
87
+ # create the project if it doesn't exist
88
+ if project_id.nil?
89
+ @machine.ui.detail "Project does not exist, creating now..."
90
+ @rancher.create_project @config.project, @config.project_type
91
+ project_id = @rancher.get_project_id @config.project
92
+ raise Errors::ProjectNotFound if project_id.nil?
93
+ end
94
+
95
+ # set the default project for the admin user
96
+ user_id = @rancher.get_admin_id
97
+ @rancher.set_default_project user_id, project_id
71
98
 
72
99
  # attempt to retrieve a registration token, otherwise create one
73
100
  unless @rancher.get_registration_token project_id
@@ -88,7 +115,7 @@ module VagrantPlugins
88
115
  # if the agent container is not running, start it
89
116
  unless @machine.communicate.test('sudo docker inspect rancher-agent')
90
117
  # retrieve the default project id
91
- project_id = @rancher.get_project_id
118
+ project_id = @rancher.get_project_id @config.project
92
119
  raise Errors::ProjectNotFound if project_id.nil?
93
120
 
94
121
  # retrieve the registration token
@@ -119,7 +146,7 @@ module VagrantPlugins
119
146
  # wait for the agent to register the host in rancher (checks
120
147
  # for the @machine.id in the host labels)
121
148
  @machine.ui.detail 'Waiting for agent to register...'
122
- unless @rancher.wait_for_agent @machine.id
149
+ unless @rancher.wait_for_agent project_id, @machine.id
123
150
  raise Errors::AgentRegistrationTimeout,
124
151
  :host => @config.hostname,
125
152
  :port => @config.port
@@ -130,7 +157,7 @@ module VagrantPlugins
130
157
  # configure the agent in rancher
131
158
  def configure_agent
132
159
  # retrieve the project id
133
- project_id = @rancher.get_project_id
160
+ project_id = @rancher.get_project_id @config.project
134
161
  raise Errors::ProjectNotFound if project_id.nil?
135
162
 
136
163
  # retrieve the host by the @machine.id label
@@ -27,10 +27,7 @@ module VagrantPlugins
27
27
  end
28
28
 
29
29
  # waits for the agent to register the host
30
- def wait_for_agent(machine_id)
31
- project_id = self.get_project_id
32
- raise Errors::ProjectNotFound if project_id.nil?
33
-
30
+ def wait_for_agent(project_id, machine_id)
34
31
  15.times do |i|
35
32
  host = self.get_host project_id, machine_id
36
33
  break unless host.nil?
@@ -42,14 +39,14 @@ module VagrantPlugins
42
39
  end
43
40
 
44
41
  # retrieves the Default project id
45
- def get_project_id
42
+ def get_project_id(name)
46
43
  project_id = nil
47
44
 
48
45
  response = self.api '/v1/projects'
49
46
 
50
47
  unless response.nil? or response['data'].empty?
51
48
  response['data'].each do |project|
52
- if project['name'] == 'Default'
49
+ if project['name'] == name
53
50
  project_id = project['id']
54
51
  end
55
52
  end
@@ -58,6 +55,17 @@ module VagrantPlugins
58
55
  project_id
59
56
  end
60
57
 
58
+ # retrieves and returns a project object
59
+ def get_project(project_id)
60
+ return self.api "/v1/projects/#{project_id}"
61
+ end
62
+
63
+ # retrieves and returns the id of the admin user
64
+ def get_admin_id
65
+ response = self.api '/v1/accounts?name=admin&kind=admin'
66
+ return response['data'][0]['id']
67
+ end
68
+
61
69
  # retrieves a rancher host object
62
70
  def get_host(project_id, machine_id)
63
71
  host = nil
@@ -82,6 +90,55 @@ module VagrantPlugins
82
90
  nil
83
91
  end
84
92
 
93
+ # creates a new project
94
+ def create_project(name, type='cattle')
95
+ swarm = false
96
+ kubernetes = false
97
+
98
+ case type
99
+ when 'kubernetes'
100
+ kubernetes = true
101
+ when 'swarm'
102
+ swarm = true
103
+ end
104
+
105
+ data = {
106
+ 'name' => name,
107
+ 'swarm' => swarm,
108
+ 'kubernetes' => kubernetes,
109
+ 'publicDns' => false,
110
+ 'members' => []
111
+ }
112
+ return self.api '/v1/project/', 'POST', nil, data
113
+ end
114
+
115
+ # sets the default project for a user
116
+ def set_default_project(user_id, project_id)
117
+ response = self.api "/v1/userpreferences?accountId=#{user_id}&name=defaultProjectId"
118
+
119
+ data = {
120
+ 'name' => 'defaultProjectId',
121
+ 'value' => project_id,
122
+ 'kind' => 'userPreference',
123
+ 'type' => 'userPreference',
124
+ 'accountId' => user_id,
125
+ }
126
+
127
+ if response['data'].empty?
128
+ self.api '/v1/userpreferences/', 'POST', nil, data
129
+ else
130
+ preference_id = response['data'][0]['id']
131
+ self.api "/v1/userpreferences/#{preference_id}/?action=update", 'POST', nil, data
132
+ end
133
+ end
134
+
135
+ # deletes a project
136
+ def delete_project(project_id)
137
+ self.api "/v1/projects/#{project_id}/?action=delete", 'POST'
138
+ sleep 2
139
+ self.api "/v1/projects/#{project_id}/?action=purge", 'POST'
140
+ end
141
+
85
142
  # creates a registration token for a project
86
143
  def create_registration_token(project_id)
87
144
  headers = { 'x-api-project-id' => project_id }
@@ -96,7 +153,7 @@ module VagrantPlugins
96
153
  # deactivates a host in rancher to avoid being scheduled on
97
154
  def deactivate_host(project_id, host_id)
98
155
  headers = { 'x-api-project-id' => project_id }
99
- self.api "/v1/projects/#{project_id}/hosts/#{host_id}/?action=deactivate", 'POST'
156
+ self.api "/v1/projects/#{project_id}/hosts/#{host_id}/?action=deactivate", 'POST', headers
100
157
  end
101
158
 
102
159
  protected
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module Rancher
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-rancher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nextrevision
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-18 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler