Class: Gantree::Create

Inherits:
Base
  • Object
show all
Defined in:
lib/gantree/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#authenticate_librato, #cfm, #check_credentials, check_for_updates, #check_template_bucket, #create_default_env, #eb, #env_type, #error_msg, #escape_characters_in_string, #get_latest_docker_solution, #print_options, #s3, #set_aws_keys, #tag, update_configuration, #upload_templates

Constructor Details

#initialize(stack_name, options) ⇒ Create

Returns a new instance of Create.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gantree/create.rb', line 11

def initialize stack_name,options
  check_credentials
  set_aws_keys

  @requirements = "#!/usr/bin/env ruby
    require 'cloudformation-ruby-dsl/cfntemplate'
    require 'cloudformation-ruby-dsl/spotprice'
    require 'cloudformation-ruby-dsl/table'"

  additional_options = {
    requirements: @requirements,
    stack_name: stack_name,
    stack_hash: (0...8).map { (65 + rand(26)).chr }.join
  }
  @options = options.merge(additional_options)
  @options[:env] ||= create_default_env
  @options[:env_type] ||= env_type
  @options[:solution] ||= get_latest_docker_solution
  @templates = ['master','resources','beanstalk']
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/gantree/create.rb', line 9

def env
  @env
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



9
10
11
# File 'lib/gantree/create.rb', line 9

def stack_name
  @stack_name
end

Instance Method Details

#create_all_templatesObject



49
50
51
# File 'lib/gantree/create.rb', line 49

def create_all_templates
  @options[:dupe] ? duplicate_stack : generate_all_templates
end

#create_aws_cfn_stackObject



93
94
95
96
97
98
99
100
# File 'lib/gantree/create.rb', line 93

def create_aws_cfn_stack
  puts "Creating stack on aws..."
  stack = cfm.stacks.create(@options[:stack_name], stack_template, { 
    :disable_rollback => true, 
    :tags => [
      { key: "StackName", value: @options[:stack_name] },
    ]})
end

#create_cfn_if_neededObject



45
46
47
# File 'lib/gantree/create.rb', line 45

def create_cfn_if_needed
  Dir.mkdir 'cfn' unless File.directory?("cfn")
end

#duplicate_stackObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gantree/create.rb', line 60

def duplicate_stack
  puts "Duplicating cluster"
  orgin_stack_name = @options[:dupe]
  @templates.each do |template|
    FileUtils.cp("cfn/#{orgin_stack_name}-#{template}.cfn.json", "cfn/#{@options[:stack_name]}-#{template}.cfn.json")
    file = IO.read("cfn/#{@options[:stack_name]}-#{template}.cfn.json")
    file.gsub!(/#{escape_characters_in_string(orgin_stack_name)}/, @options[:stack_name])
    replace_env_references(file)
    IO.write("cfn/#{@options[:stack_name]}-#{template}.cfn.json",file)
  end
end

#generate(template_name, template) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/gantree/create.rb', line 83

def generate(template_name, template)
  IO.write("cfn/#{template_name}.rb", template)
  json = `ruby cfn/#{template_name}.rb expand`
  Dir.mkdir 'cfn' rescue Errno::ENOENT
  template_file_name = "#{@options[:stack_name]}-#{template_name}.cfn.json"
  IO.write("cfn/#{template_file_name}", json)
  puts "Created #{template_file_name} in the cfn directory"
  FileUtils.rm("cfn/#{template_name}.rb")
end

#generate_all_templatesObject



53
54
55
56
57
58
# File 'lib/gantree/create.rb', line 53

def generate_all_templates
  puts "Generating templates from gantree"
  generate("master", MasterTemplate.new(@options).create)
  generate("beanstalk", BeanstalkTemplate.new(@options).create)
  generate("resources", ResourcesTemplate.new(@options).create)
end

#rds_enabled?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gantree/create.rb', line 102

def rds_enabled?
  if @options[:rds] == nil
    puts "RDS is not enabled, no DB created"
    false
  elsif @options[:rds] == "pg" || @rds == "mysql"
    puts "RDS is enabled, creating DB"
    true
  else
    raise "The --rds option you passed is not supported please use 'pg' or 'mysql'"
  end
end

#replace_env_references(file) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/gantree/create.rb', line 72

def replace_env_references file
  origin_tags = @options[:dupe].split("-")
  new_tags = @options[:stack_name].split("-")
  possible_roles = ["app","worker","listener","djay","scheduler","sched","list","lisnr","listnr"]
  possible_roles.each do |role|
    origin_env = [origin_tags[1],origin_tags[0],role,origin_tags[2]].join('-')
    new_env = [new_tags[1],new_tags[0],role,new_tags[2]].join('-')
    file.gsub!(/#{escape_characters_in_string(origin_env)}/, new_env)
  end
end

#runObject



32
33
34
35
36
37
38
39
# File 'lib/gantree/create.rb', line 32

def run
  @options[:rds_enabled] = rds_enabled? if @options[:rds] 
  print_options
  create_cfn_if_needed
  create_all_templates unless @options[:local]
  upload_templates unless @options[:dry_run]
  create_aws_cfn_stack unless @options[:dry_run]
end

#stack_templateObject



41
42
43
# File 'lib/gantree/create.rb', line 41

def stack_template
  s3.buckets["#{@options[:cfn_bucket]}/#{@options[:stack_name]}"].objects["#{@options[:stack_name]}-master.cfn.json"]
end