# -*- encoding: utf-8 -*- # # Author:: Anthony Howell (anthonyrhowell@gmail.com) # # Copyright (C) 2015, Anthony Howell # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'kitchen' require 'clc_client' require 'highline/import' module Kitchen module Driver # Centurylink driver for Kitchen. # # @author Anthony Howell class Centurylink < Kitchen::Driver::SSHBase default_config :username, 'root' default_config :password, 'Kitchen123' default_config :server_name, 'test' default_config :accountAlias, 'DFT' default_config :cpu, 2 default_config :memoryGB, 4 default_config :serverTemplate, 'CENTOS-6-64-TEMPLATE' default_config :type, 'standard' default_config :assign_public_ip, false @@client = ClcClient def create(state) if !config[:token] && !state[:token] puts 'No CLC API token was found. Please enter your credentials to have one generated.' generateToken(state) end if !config[:groupId] puts 'No groupId was set. Please set groupId in .kitchen.yml and try again' puts 'See --> http://www.centurylinkcloud.com/api-docs/v2/#servers-create-server#request' raise 'Incorrect config' end @@client.setToken(config[:token] || state[:token]) queueId = create_server serverInfo = @@client.getServerDetails(config[:accountAlias], queueId + '?uuid=True') info 'Checking server status before continuing' while serverInfo['status'] != 'active' do info "Server not up. Server status is: #{serverInfo['status']}." serverInfo = @@client.getServerDetails(config[:accountAlias], queueId + '?uuid=True') sleep 20 end info serverInfo state[:server_id] = serverInfo["id"] info "Created server with id #{state[:server_id]}" if config[:assign_public_ip] info 'Assigning public IP' assign_public_ip(state) else state[:hostname] = serverInfo['details']['ipAddresses'][0]['internal'] end end def destroy(state) info "Sending delete request for server #{state[:server_id]}" @@client.setToken(config[:token] || state[:token]) response = @@client.deleteServer(config[:accountAlias], state[:server_id]) if response['isQueued'] == true info 'Delete request is queued up' state.delete(:server_id) state.delete(:hostname) end end def create_server info 'Sending create server request' response = @@client.createServer(config[:accountAlias], { :name => config[:server_name], :groupId => config[:groupId], :cpu => config[:cpu], :memoryGB => config[:memoryGB], :sourceServerId => config[:serverTemplate], :type => config[:type], :password => config[:password], }) debug response serverId = response['links'].select {|x| x['rel']=="self"}[0]['id'] puts "Server request accepted, with request id #{serverId}" serverId end def assign_public_ip(state) response = @@client.addPublicIpAddress(config[:accountAlias], state[:server_id], { :ports => [ {:protocol => 'TCP', :port => 80}, {:protocol => 'TCP', :port => 8080}, {:protocol => 'TCP', :port => 443}, {:protocol => 'TCP', :port => 22}, ]}) puts response statusId = response['id'] status = @@client.getQueueStatus(config[:accountAlias], statusId) while status['status'] != 'succeeded' do info "Waiting for public IP job to complete. Status is #{status["status"]}" sleep 20 status = @@client.getQueueStatus(config[:accountAlias], statusId) end serverInfo = @@client.getServerDetails(config[:accountAlias], state[:server_id]) state[:hostname] = serverInfo['details']['ipAddresses'].find {|ipSet| ipSet['public'] }['public'] info "Assigned public IP of: #{state[:hostname]}" end def generateToken(state) if !config[:clc_username] username = ask 'Username:' end password = ask ('Password:') { |q| q.echo = false } token = @@client.login username, password info 'Generated new token:' info token info "Save this to your project's .kitchen.yml file to use for future logins" state[:token] = token end end end end