Class: VagrantPlugins::Rancher::Provisioner
- Inherits:
-
Object
- Object
- VagrantPlugins::Rancher::Provisioner
- Defined in:
- lib/vagrant-rancher/provisioner.rb
Instance Method Summary collapse
-
#check_docker ⇒ Object
determines if we can reach the docker daemon.
-
#configure_agent ⇒ Object
configure the agent in rancher.
-
#configure_server ⇒ Object
configures a running server with the required settings.
-
#initialize(machine, config, rancher = nil) ⇒ Provisioner
constructor
A new instance of Provisioner.
-
#install_agent ⇒ Object
runs the agent container on the guest.
-
#install_server ⇒ Object
installs the rancher server at the supplied version.
- #provision ⇒ Object
Constructor Details
#initialize(machine, config, rancher = nil) ⇒ Provisioner
Returns a new instance of Provisioner.
7 8 9 10 |
# File 'lib/vagrant-rancher/provisioner.rb', line 7 def initialize(machine, config, rancher = nil) super(machine, config) @rancher = rancher || RancherClient.new(@config.hostname, @config.port) end |
Instance Method Details
#check_docker ⇒ Object
determines if we can reach the docker daemon
23 24 25 26 27 28 |
# File 'lib/vagrant-rancher/provisioner.rb', line 23 def check_docker unless @machine.communicate.test('sh -c sleep 3; sudo docker info') @machine.ui.error 'Could not connect to docker daemon' raise Errors::DockerConnection end end |
#configure_agent ⇒ Object
configure the agent in rancher
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/vagrant-rancher/provisioner.rb', line 181 def configure_agent # retrieve the project id project_id = @rancher.get_project_id @config.project raise Errors::ProjectNotFound if project_id.nil? # retrieve the host by the @machine.id label host = @rancher.get_host project_id, @machine.id raise Errors::HostNotFound, :project_id => project_id if host.nil? # when deactivate is set and the host is active, deactivate it if @config.deactivate and host['state'] == 'active' @machine.ui.info "Deactivating agent..." @rancher.deactivate_host project_id, host['id'] end end |
#configure_server ⇒ Object
configures a running server with the required settings
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 |
# File 'lib/vagrant-rancher/provisioner.rb', line 75 def configure_server # get the specified project project_id = @rancher.get_project_id @config.project # verify the project is of the correct type unless project_id.nil? recreate = false project = @rancher.get_project project_id if project['kubernetes'] and @config.project_type != 'kubernetes' recreate = true elsif project['swarm'] and @config.project_type != 'swarm' recreate = true elsif @config.project_type == 'cattle' and (project['kubernetes'] or project['swarm']) recreate = true end # destroy the project and set project_id to nil when # the project is not the specified type if recreate @rancher.delete_project project_id project_id = nil end end # create the project if it doesn't exist if project_id.nil? @machine.ui.detail "Project does not exist, creating now..." @rancher.create_project @config.project, @config.project_type sleep 2 project_id = @rancher.get_project_id @config.project raise Errors::ProjectNotFound if project_id.nil? end # set the default project for the admin user user_id = @rancher.get_admin_id @rancher.set_default_project user_id, project_id # attempt to retrieve a registration token, otherwise create one unless @rancher.get_registration_token project_id @machine.ui.detail "Registration tokens have not been created, creating now..." @rancher.create_registration_token project_id # set the api.host setting required for agents @rancher.configure_setting 'api.host', "http://#{@config.hostname}:#{@config.port}" sleep(2) # verify that the registration token was created raise Errors::RegistrationTokenMissing unless @rancher.get_registration_token project_id end end |
#install_agent ⇒ Object
runs the agent container on the guest
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 |
# File 'lib/vagrant-rancher/provisioner.rb', line 127 def install_agent # if the agent container is not running, start it unless @machine.communicate.test('sudo docker inspect rancher-agent') # retrieve the default project id project_id = @rancher.get_project_id @config.project raise Errors::ProjectNotFound if project_id.nil? # retrieve the registration token @machine.ui.detail 'Retrieving agent registration command...' registration_token = @rancher.get_registration_token project_id raise Errors::RegistrationTokenMissing if registration_token.nil? docker_cmd = registration_token['command'] # apply a default label with the machine id used for # checking that the agent has indeed registered the host labels = "id=#{@machine.id}" # apply and additional host labels unless config.labels.nil? labels = "#{labels}&#{config.labels.join('&')}" end extra_args = "-e 'CATTLE_HOST_LABELS=#{labels}' --name rancher-agent-bootstrap" docker_cmd = docker_cmd.sub('docker run', "docker run #{extra_args}") # pull rancher agent image if its not already there image_check_cmd = "sudo docker images | awk '{ print $1\":\"$2 }' | grep -q #{registration_token['image']}" unless @machine.communicate.test(image_check_cmd) @machine.ui.info "Pulling Rancher agent image: #{registration_token['image']}..." unless @machine.communicate.sudo("docker pull #{registration_token['image']}") @machine.ui.error "Could not pull Rancher agent image" raise Errors::RancherServerContainer end end # start the agent container @machine.ui.info "Starting agent container: #{registration_token['image']}..." unless @machine.communicate.sudo(docker_cmd) @machine.ui.error 'Could not start Rancher agent container' raise Errors::RancherAgentContainer end # wait for the agent to register the host in rancher (checks # for the @machine.id in the host labels) @machine.ui.detail 'Waiting for agent to register...' unless @rancher.wait_for_agent project_id, @machine.id raise Errors::AgentRegistrationTimeout, :host => @config.hostname, :port => @config.port end end end |
#install_server ⇒ Object
installs the rancher server at the supplied version
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 |
# File 'lib/vagrant-rancher/provisioner.rb', line 31 def install_server # check to see if the rancher server is already running unless @machine.communicate.test('sudo docker inspect rancher-server') image = "#{config.rancher_server_image}:#{config.version}" # pull rancher server image if its not already there image_check_cmd = "sudo docker images | awk '{ print $1\":\"$2 }' | grep -q #{image}" unless @machine.communicate.test(image_check_cmd) @machine.ui.info "Pulling Rancher server image: #{image}..." unless @machine.communicate.sudo("docker pull #{image}") @machine.ui.error "Could not pull Rancher server image" raise Errors::RancherServerContainer end end @machine.ui.info "Starting server container: #{image}..." docker_cmd = "docker run -d -t --name rancher-server -p #{@config.port}:8080" # add any user supplied args to the docker command unless config.server_args.nil? docker_cmd = "#{docker_cmd} #{config.server_args}" end # start the server and error if there is a failure unless @machine.communicate.sudo("#{docker_cmd} #{image}") @machine.ui.error 'Could not start Rancher server container' raise Errors::RancherServerContainer end # wait for the server api to come online @machine.ui.detail 'Waiting for server API to become available...' unless @rancher.wait_for_api raise Errors::ApiConnectionTimeout, :host => @config.hostname, :port => @config.port end # gratuitous pause for the server api sleep 5 end end |
#provision ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/vagrant-rancher/provisioner.rb', line 12 def provision self.check_docker self.install_server if config.role == 'server' self.configure_server self.install_agent self.configure_agent end |