Class: Gantree::Base

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

Direct Known Subclasses

App, Create, Delete, Deploy, Docker, Init, Update

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_for_updates(opts) ⇒ Object



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

def self.check_for_updates opts
  puts opts.inspect
  enabled =  opts[:auto_updates]
  #return if $0.include? "bin/gantree"
  latest_version = `gem search gantree | grep gantree | awk '{ print $2 }' | tr -d '()'`.strip
  current_version = VERSION
  puts "Auto updates enabled".light_blue if enabled
  puts "Auto updates disabled".light_blue if !enabled
  puts "Updating from #{current_version} to #{latest_version}...".green
  if  current_version == latest_version && enabled
    system("gem update gantree --force") 
  else 
    puts "gem already up to date".light_blue
  end
   update_configuration(opts[:auto_configure]) if opts[:auto_configure]
end

.update_configuration(repo) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/gantree/base.rb', line 29

def self.update_configuration repo
  puts "Auto configuring from #{repo}".light_blue
  FileUtils.rm_rf("/tmp/gantreecfg") if File.directory?("/tmp/gantreecfg")
  system("git clone #{repo} /tmp/gantreecfg")
  FileUtils.cp("/tmp/gantreecfg/.gantreecfg","#{ENV['HOME']}/")
  puts "Auto Configured".green
end

Instance Method Details

#authenticate_libratoObject



81
82
83
84
85
# File 'lib/gantree/base.rb', line 81

def authenticate_librato
  if @options[:librato]
    Librato::Metrics.authenticate @options[:librato][:email], @options[:librato][:token]
  end
end

#cfmObject



58
59
60
# File 'lib/gantree/base.rb', line 58

def cfm
  @cfm ||= AWS::CloudFormation.new
end

#check_credentialsObject



7
8
9
10
# File 'lib/gantree/base.rb', line 7

def check_credentials
  raise "Please set your AWS Environment Variables" unless ENV['AWS_SECRET_ACCESS_KEY']
  raise "Please set your AWS Environment Variables" unless ENV['AWS_ACCESS_KEY_ID']
end

#check_template_bucketObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gantree/base.rb', line 120

def check_template_bucket
  puts "DEBUG: #{@options[:cfn_bucket]}"
  raise "Set Bucket to Upload Templates with --cfn-bucket" unless @options[:cfn_bucket]
  bucket_name = "#{@options[:cfn_bucket]}/#{@options[:stack_name]}"
  if s3.buckets[bucket_name].exists?
    puts "uploading cfn templates to #{@options[:cfn_bucket]}/#{@options[:stack_name]}"
  else
    puts "creating bucket #{@options[:cfn_bucket]}/#{@options[:stack_name]} to upload templates"
    s3.buckets.create(bucket_name) 
  end
end

#create_default_envObject



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

def create_default_env
  tags = @options[:stack_name].split("-")
  if tags.length == 3
    [tags[1],tags[0],"app",tags[2]].join('-')
  else
    raise "Please Set Envinronment Name with -e"
  end
end

#ebObject



54
55
56
# File 'lib/gantree/base.rb', line 54

def eb
  @eb ||= AWS::ElasticBeanstalk::Client.new
end

#env_typeObject



87
88
89
90
91
92
93
94
95
# File 'lib/gantree/base.rb', line 87

def env_type
  if @options[:env].include?("prod")
    "prod"
  elsif @options[:env].include?("stag")
    "stag"
  else
    ""
  end
end

#error_msg(msg) ⇒ Object



132
133
134
135
# File 'lib/gantree/base.rb', line 132

def error_msg msg
  puts msg.red
  exit 1
end

#escape_characters_in_string(string) ⇒ Object



105
106
107
108
# File 'lib/gantree/base.rb', line 105

def escape_characters_in_string(string)
  pattern = /(\'|\"|\.|\*|\/|\-|\\)/
  string.gsub(pattern){|match|"\\"  + match} # <-- Trying to take the currently found match and add a \ before it I have no idea how to do that).
end

#get_latest_docker_solutionObject



97
98
99
100
101
102
# File 'lib/gantree/base.rb', line 97

def get_latest_docker_solution
  result = eb.list_available_solution_stacks
  solutions = result[:solution_stacks]
  docker_solutions = solutions.select { |s|  s.include? "running Docker"}
  docker_solutions.first
end


37
38
39
40
41
# File 'lib/gantree/base.rb', line 37

def print_options
  @options.each do |param, value|
    puts "#{param}: #{value}"
  end
end

#s3Object



50
51
52
# File 'lib/gantree/base.rb', line 50

def s3
  @s3 ||= AWS::S3.new
end

#set_aws_keysObject



43
44
45
46
47
48
# File 'lib/gantree/base.rb', line 43

def set_aws_keys
  AWS.config(
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  )
end

#tagObject



63
64
65
66
67
68
69
70
# File 'lib/gantree/base.rb', line 63

def tag
  origin = `git config --get remote.origin.url`.match("com(.*)\/")[1].gsub(":","").gsub("/","").strip
  branch = `git rev-parse --abbrev-ref HEAD`.strip
  hash = `git rev-parse --verify --short #{branch}`.strip
  "#{origin}-#{branch.gsub('-','')}-#{hash}".gsub("/", "").downcase
  rescue
    puts "ERROR: Using outside of a git repository".red
end

#upload_templatesObject



110
111
112
113
114
115
116
117
118
# File 'lib/gantree/base.rb', line 110

def upload_templates
  check_template_bucket
  @templates.each do |template|
    filename = "cfn/#{@options[:stack_name]}-#{template}.cfn.json"
    key = File.basename(filename)
    s3.buckets["#{@options[:cfn_bucket]}/#{@options[:stack_name]}"].objects[key].write(:file => filename)
  end
  puts "templates uploaded"
end