class Object
Public Instance Methods
awsBucket(region, bucketName)
click to toggle source
# File lib/ecs-kingpin/aws.rb, line 5 def awsBucket(region, bucketName) # connect to s3 s3 = Aws::S3::Client.new( region: region ) buckets = s3.list_buckets.to_h buckets.extend(Hashie::Extensions::DeepLocate) bucket = buckets.deep_locate -> (key, value, _object) { key == :name && value.include?(bucketName) } if bucket.empty? Kinglog.log.info "Creating bucket #{bucketName}!" s3.create_bucket(bucket: bucketName) Kinglog.log.info "Enabling bucket versioning!" s3.put_bucket_versioning(bucket: bucketName, versioning_configuration: { mfa_delete: 'Disabled', status: 'Enabled' }) else Kinglog.log.info "Bucket allready exists" end end
cliOpts()
click to toggle source
# File lib/ecs-kingpin/options.rb, line 6 def cliOpts opts = Trollop.options do version 'ecs-kingpin 0.1.0' banner <<-EOS Manage your ECS resources. Usage: ecs-kinpin [options] where [options] are: EOS opt :region, 'Put your aws region here', type: :string opt :env, 'Application Environment', type: :string opt :product, 'The product where your services are related', type: :string opt :command, 'Use aws cli or terraform to create service', type: :string, default: 'aws' opt :file, 'Yaml file to read data from', type: :string opt :local, 'Use local config files', type: :bool, default: false end # Check for options needed Trollop.die :region, 'Define a region' if opts[:region].nil? Trollop.die :env, 'Define an environment' if opts[:env].nil? Trollop.die :product, 'Define a product' if opts[:product].nil? Trollop.die :file, 'Define a file' if opts[:file].nil? # Merge filepath + filename to opts opts[:file] = File.expand_path(opts[:file]) # Check if file exists unless File.exist? (opts[:file]) Kinglog.log.error 'File does not exist' abort end # Check valid extention accepted_formats = ['.yml', '.yaml'] unless accepted_formats.include? File.extname(opts[:file]) Kinglog.log.error 'Not a valid yaml extention' abort end # Check valid yaml Kinglog.log.info "Validating yaml file : #{opts[:file]}" if YAML.load_file(opts[:file]) == false Kinglog.log.error "failed to read #{opts[:file]}: #{$ERROR_INFO}" abort end # Puts all file options into opts[:yaml] # Accessable trough opts[:yaml]['metadata'] .... opts[:yaml] = fileOpts(opts[:file]) opts end
connectEcs(region)
click to toggle source
# File lib/ecs-kingpin/aws.rb, line 1 def connectEcs(region) Aws::ECS::Client.new(region: region) end
createServiceVars(array, ports)
click to toggle source
Create service.tfvars for terraform
# File lib/ecs-kingpin/service.rb, line 2 def createServiceVars(array, ports) product = array[:product] service = array[:yaml]['metadata']['name'] env = array[:env] region = array[:region] port = ports[0][0]['containerPort'] containerPort = ports[0][0]['port'] type = array[:yaml]['type'] serviceVars = "product: <%= product %> service: <%= service %> env: <%= env %> region: <%= region %> elb_port: <%= port %> container_port: <%= containerPort %> <% if type == \"PublicService\" %>service_type: public<% else %>service_type: private<% end %>" # Create a service.tfvars renderer = ERB.new(serviceVars) File.open('service.tfvars', 'w') do |f| f.write renderer.result(binding) end Kinglog.log.info 'Wrote service.tfvars' end
createTaskDefinition(array, service)
click to toggle source
Create task-definition for terraform
# File lib/ecs-kingpin/task.rb, line 2 def createTaskDefinition(array, service) json_opts = array.to_json parsed = JSON.parse(json_opts) last = parsed.last task_definition = "{\"container_definitions\": [ <% parsed.each do |array| %><%= array.to_json %><% unless last[\"name\"] == array[\"name\"] %>,<% end %><% end %> ], \"family\": \"<%= service %>\" }" # Create a task-definition.json renderer = ERB.new(task_definition) File.open('task-definition.json', 'w') do |f| f.write renderer.result(binding) end Kinglog.log.info 'Wrote task-defintion.json' end
fileOpts(file)
click to toggle source
# File lib/ecs-kingpin/options.rb, line 1 def fileOpts(file) yaml = YAML.load_file(file.to_s) yaml end
parseContainers(array, labels)
click to toggle source
# File lib/ecs-kingpin/parse.rb, line 11 def parseContainers(array, labels) arr = [] array.each do |l| c = {} c['name'] = l['name'] c['essential'] = l['essential'] c['image'] = l['image'] c['environment'] = l['env'] c['memory'] = l['resources']['memory'] c['cpu'] = l['resources']['cpu'] c['mountPoints'] = l['volumes'] c['docker_labels'] = labels c['links'] = l['links'] c['logConfiguration'] = l['logs'] arr << c end arr end
parseLabels(array)
click to toggle source
# File lib/ecs-kingpin/parse.rb, line 1 def parseLabels(array) hash = {} array.each do |l| key = l['name'] value = l['value'] hash[key] = value end hash end
parseService(array)
click to toggle source
# File lib/ecs-kingpin/parse.rb, line 30 def parseService(array) arr = [] array.each do |l| arr << l['ports'] unless l['ports'].nil? end arr end
serviceOpts()
click to toggle source
Create service options for aws api
# File lib/ecs-kingpin/service.rb, line 27 def serviceOpts end
taskOpts()
click to toggle source
Create task options for aws api
# File lib/ecs-kingpin/task.rb, line 22 def taskOpts end
terraformCleanup()
click to toggle source
Clean up all temporary files from terraform
# File lib/ecs-kingpin/terraform.rb, line 39 def terraformCleanup FileUtils.rm_rf('terraform.tfstate') FileUtils.rm_rf('terraform.tfstate.backup') FileUtils.rm_rf('mkmf.log') FileUtils.rm_rf('.terraform') FileUtils.rm_rf('task-definition.json') FileUtils.rm_rf('service.tfvars') Kinglog.log.info 'Removed all temporary files' end
terraformRun()
click to toggle source
Run terraform to apply config
# File lib/ecs-kingpin/terraform.rb, line 27 def terraformRun tf = "terraform apply" tfget = "terraform get" system tfget system tf if $? != 0 Kinglog.log.error "Can't run terraform config. Error: #{$?}" abort end end
terrformConfig(array,bucketName)
click to toggle source
Configure remote s3 bucket for terraform state file
# File lib/ecs-kingpin/terraform.rb, line 11 def terrformConfig(array,bucketName) product = array[:product] service = array[:yaml]['metadata']['name'] env = array[:env] region = array[:region] serviceState = "#{service}-#{product}-#{env}.tfstate" tfconfig = "terraform remote config -backend=s3 -backend-config=\"bucket=#{bucketName}\" \ -backend-config=\"key=#{serviceState}\" -backend-config=\"region=#{region}\"" system tfconfig if $? != 0 Kinglog.log.error "Could not apply terraform config. Errorcode #{$?}" abort end end
terrformExecCheck()
click to toggle source
Check if terraform is installed on the system
# File lib/ecs-kingpin/terraform.rb, line 2 def terrformExecCheck `which terraform` if $? == false Kinglog.log.error 'Terrform executable not found, please install terraform' abort end end