Class: DEIS::Bootstrap

Inherits:
Base
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/rdeis/bootstrap.rb

Constant Summary

Constants inherited from Base

DEIS::Base::NO, DEIS::Base::YES

Instance Attribute Summary collapse

Attributes inherited from Base

#cluster, #environment, #output, #path, #verbose

Instance Method Summary collapse

Methods inherited from Base

#local_profile, #local_shell, #local_shell_env_include, #log, #remote_profile, #run

Constructor Details

#initialize(options) ⇒ Bootstrap

Returns a new instance of Bootstrap.



7
8
9
10
11
12
# File 'lib/rdeis/bootstrap.rb', line 7

def initialize(options)
  @repo = DEIS::Git.name
  @verbose = options[:verbose]
  @environment = options[:environment]
  say( "Checking local environment..", :green)
end

Instance Attribute Details

#deploy_toObject

Returns the value of attribute deploy_to.



5
6
7
# File 'lib/rdeis/bootstrap.rb', line 5

def deploy_to
  @deploy_to
end

#proxyObject

Returns the value of attribute proxy.



5
6
7
# File 'lib/rdeis/bootstrap.rb', line 5

def proxy
  @proxy
end

#sshObject

Returns the value of attribute ssh.



5
6
7
# File 'lib/rdeis/bootstrap.rb', line 5

def ssh
  @ssh
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/rdeis/bootstrap.rb', line 5

def token
  @token
end

Instance Method Details

#allObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rdeis/bootstrap.rb', line 53

def all
  @proxy = nil
  @token = nil
  file = DEIS::Storage::BASE_PATH.gsub("$HOME", ENV['HOME'])+".profile"
  if ! File.exists?(file)
    self.proxy
    @ssh = SSH.new({:host => @proxy, :user => nil}) if ! @proxy.nil?
    self.hush
    self.github_token
    self.deploy_path
    self.save
    @ssh.close
    say "Bootstrap Completed", :blue
  end
end

#deploy_pathObject



38
39
40
41
42
43
44
# File 'lib/rdeis/bootstrap.rb', line 38

def deploy_path
  if @proxy.nil?
    @deploy_to = ask("Please enter local folder to deploy to: ", :yellow)
  else
    @deploy_to = ask("Please enter containing folder on #{@proxy} to deploy to: ", :yellow)
  end
end

#github_tokenObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rdeis/bootstrap.rb', line 20

def github_token
  # if local env doesnt have it and no proxy host, just ask
  if ENV[DEIS::Git.github_token_varname].nil? && @proxy.nil?
    @token = ask("Please enter your github auth token: ", :yellow)
  # if proxy is set, then check for the var on the proxy:key => "value",
  elsif ENV[DEIS::Git.github_token_varname].nil? && ! @proxy.nil?
    cmd = DEIS::Git.github_token_cmd
    say("[#{Time.now.to_s}] "+cmd+"\n", :black) if @verbose
    remote = @ssh.exec!(cmd).to_s.strip
    if ! remote.nil? && remote.length > 0 && (yes?("Found #{remote} on #{@proxy}, set as local version? (y/n): ", :yellow) )
      @token = remote
    end
  else
   @token = ask("Please enter a github auth token for deployment: ", :yellow)
  end

end

#hushObject

add a hush login file so we dont get the MOTD ouput in our shell commands



47
48
49
50
51
# File 'lib/rdeis/bootstrap.rb', line 47

def hush
  if yes?("Does the deploy location run a Message of the Day? ", :yellow)
    run("if [[ ! -f $HOME/.hushlogin ]]; then > $HOME/.hushlogin ; fi ")
  end
end

#saveObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rdeis/bootstrap.rb', line 69

def save
  # the base file location for the env vars we need
  file = DEIS::Storage::ENV_VAR_PATH
  replaced = DEIS::Storage::BASE_PATH.gsub("$HOME", ENV['HOME'])
  FileUtils.mkdir_p(replaced) if ! File.directory?(replaced)
  out = "# [rdeis #{DEIS::VERSION}] Auto generated at #{Time.now.to_s}\n"
  out = out+"export RDEIS_PROXY=\"#{@proxy}\"\n" if ! @proxy.nil?
  out = out+"export RDEIS_PATH=\"#{@deploy_to.gsub('$', '\$') }\"\n" if ! @deploy_to.nil?
  out = out+"export #{DEIS::Git.github_token_varname}=\"#{@token}\"\n" if ! @token.nil?
  # write locally
  File.open(file.gsub("$HOME", ENV['HOME']), "w"){ |f| f.write(out) }
  # run remote, keep the $HOME notation as we want that evaluated in the cmd
  run("mkdir -p #{DEIS::Storage::BASE_PATH} ; echo -e '#{out}' > #{file}") if ! @proxy.nil?
  # remove any existing references from the various shell options on the local machine
  shell = self.local_shell
  profile = self.local_profile
  if ! profile.nil?
    self.local_shell_env_include(profile, file)
  else
    say "Could not save environment setup", :red
  end
  # add include to the remote profile
  run("echo -e '. #{file}\n' >> #{self.remote_profile}") if ! @proxy.nil?
end