Class: VagrantPlugins::Rancher::RancherClient
- Inherits:
-
Object
- Object
- VagrantPlugins::Rancher::RancherClient
- Defined in:
- lib/vagrant-rancher/rancher.rb
Instance Method Summary collapse
-
#configure_setting(setting, value) ⇒ Object
configures rancher settings.
-
#create_project(name, type = 'cattle') ⇒ Object
creates a new project.
-
#create_registration_token(project_id) ⇒ Object
creates a registration token for a project.
-
#deactivate_host(project_id, host_id) ⇒ Object
deactivates a host in rancher to avoid being scheduled on.
-
#delete_project(project_id) ⇒ Object
deletes a project.
-
#get_admin_id ⇒ Object
retrieves and returns the id of the admin user.
-
#get_host(project_id, machine_id) ⇒ Object
retrieves a rancher host object.
-
#get_project(project_id) ⇒ Object
retrieves and returns a project object.
-
#get_project_id(name) ⇒ Object
retrieves the Default project id.
-
#get_registration_token(project_id) ⇒ Object
retrieves a registration token for a project.
-
#initialize(hostname, port) ⇒ RancherClient
constructor
A new instance of RancherClient.
-
#set_default_project(user_id, project_id) ⇒ Object
sets the default project for a user.
-
#wait_for_agent(project_id, machine_id) ⇒ Object
waits for the agent to register the host.
-
#wait_for_api ⇒ Object
waits for the rancher api to come online.
Constructor Details
#initialize(hostname, port) ⇒ RancherClient
Returns a new instance of RancherClient.
7 8 9 10 |
# File 'lib/vagrant-rancher/rancher.rb', line 7 def initialize(hostname, port) @hostname = hostname @port = port end |
Instance Method Details
#configure_setting(setting, value) ⇒ Object
configures rancher settings
149 150 151 |
# File 'lib/vagrant-rancher/rancher.rb', line 149 def configure_setting(setting, value) self.api "/v1/activesettings/#{setting}", 'PUT', nil, 'value' => value end |
#create_project(name, type = 'cattle') ⇒ Object
creates a new project
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/vagrant-rancher/rancher.rb', line 94 def create_project(name, type='cattle') swarm = false kubernetes = false case type when 'kubernetes' kubernetes = true when 'swarm' swarm = true end data = { 'name' => name, 'swarm' => swarm, 'kubernetes' => kubernetes, 'publicDns' => false, 'members' => [] } return self.api '/v1/project/', 'POST', nil, data end |
#create_registration_token(project_id) ⇒ Object
creates a registration token for a project
143 144 145 146 |
# File 'lib/vagrant-rancher/rancher.rb', line 143 def create_registration_token(project_id) headers = { 'x-api-project-id' => project_id } self.api '/v1/registrationtokens/', 'POST', headers end |
#deactivate_host(project_id, host_id) ⇒ Object
deactivates a host in rancher to avoid being scheduled on
154 155 156 157 |
# File 'lib/vagrant-rancher/rancher.rb', line 154 def deactivate_host(project_id, host_id) headers = { 'x-api-project-id' => project_id } self.api "/v1/projects/#{project_id}/hosts/#{host_id}/?action=deactivate", 'POST', headers end |
#delete_project(project_id) ⇒ Object
deletes a project
136 137 138 139 140 |
# File 'lib/vagrant-rancher/rancher.rb', line 136 def delete_project(project_id) self.api "/v1/projects/#{project_id}/?action=delete", 'POST' sleep 2 self.api "/v1/projects/#{project_id}/?action=purge", 'POST' end |
#get_admin_id ⇒ Object
retrieves and returns the id of the admin user
64 65 66 67 |
# File 'lib/vagrant-rancher/rancher.rb', line 64 def get_admin_id response = self.api '/v1/accounts?name=admin&kind=admin' return response['data'][0]['id'] end |
#get_host(project_id, machine_id) ⇒ Object
retrieves a rancher host object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vagrant-rancher/rancher.rb', line 70 def get_host(project_id, machine_id) host = nil response = self.api "/v1/projects/#{project_id}/hosts" unless response.nil? or response['data'].empty? response['data'].each do |h| host = h if h['labels'].values.include? machine_id end end host end |
#get_project(project_id) ⇒ Object
retrieves and returns a project object
59 60 61 |
# File 'lib/vagrant-rancher/rancher.rb', line 59 def get_project(project_id) return self.api "/v1/projects/#{project_id}" end |
#get_project_id(name) ⇒ Object
retrieves the Default project id
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vagrant-rancher/rancher.rb', line 42 def get_project_id(name) project_id = nil response = self.api '/v1/projects' unless response.nil? or response['data'].empty? response['data'].each do |project| if project['name'] == name project_id = project['id'] end end end project_id end |
#get_registration_token(project_id) ⇒ Object
retrieves a registration token for a project
85 86 87 88 89 90 91 |
# File 'lib/vagrant-rancher/rancher.rb', line 85 def get_registration_token(project_id) headers = { 'x-api-project-id' => project_id } response = self.api '/v1/registrationtokens/', 'GET', headers return response['data'][0] unless response['data'].empty? nil end |
#set_default_project(user_id, project_id) ⇒ Object
sets the default project for a user
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/vagrant-rancher/rancher.rb', line 116 def set_default_project(user_id, project_id) response = self.api "/v1/userpreferences?accountId=#{user_id}&name=defaultProjectId" data = { 'name' => 'defaultProjectId', 'value' => project_id, 'kind' => 'userPreference', 'type' => 'userPreference', 'accountId' => user_id, } if response['data'].empty? self.api '/v1/userpreferences/', 'POST', nil, data else preference_id = response['data'][0]['id'] self.api "/v1/userpreferences/#{preference_id}/?action=update", 'POST', nil, data end end |
#wait_for_agent(project_id, machine_id) ⇒ Object
waits for the agent to register the host
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vagrant-rancher/rancher.rb', line 30 def wait_for_agent(project_id, machine_id) 15.times do |i| host = self.get_host project_id, machine_id break unless host.nil? return false if i == 15 sleep 10 end true end |
#wait_for_api ⇒ Object
waits for the rancher api to come online
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vagrant-rancher/rancher.rb', line 13 def wait_for_api uri = URI "http://#{@hostname}:#{@port}/v1" 15.times do |i| begin Net::HTTP.get_response uri break rescue return false if i == 15 sleep 10 end end true end |