Class: AWS_SSH::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_ssh/setup.rb

Direct Known Subclasses

Teardown

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#profileObject

Returns the value of attribute profile.



10
11
12
# File 'lib/aws_ssh/setup.rb', line 10

def profile
  @profile
end

Class Method Details

.shells(home, shell) ⇒ Object



24
25
26
# File 'lib/aws_ssh/setup.rb', line 24

def self.shells(home, shell)
  ["#{home}/.#{shell}rc", "#{home}/.#{shell}_profile", "#{home}/.profile"]
end

Instance Method Details

#add_to_profileObject



57
58
59
60
61
62
63
64
# File 'lib/aws_ssh/setup.rb', line 57

def add_to_profile
  if ! @profile.nil?
    contents = File.open(@profile, 'rb') {|file| file.read}
    # remove any current references and add new one in
    contents = contents.gsub(". ~/#{AWS_SSH::ALIAS_FILE}", "").chomp + "\n" + ". ~/#{AWS_SSH::ALIAS_FILE}"
    File.open(@profile, 'w') { |file| file.write(contents) }
  end
end

#aliasObject

create the alias file



41
42
43
44
45
46
47
# File 'lib/aws_ssh/setup.rb', line 41

def alias
  msg = "# This is an automatically generated file; do not edit.\n# Created at #{Time.now}\n# Created by aws_ssh #{AWS_SSH::VERSION}\n"
  cmd = 'alias ssh="' + AWS_SSH::GENERATE_CMD + ' ; '+AWS_SSH::MERGE_CMD+ '; ssh"'
  filename=ENV['HOME']+"/"+AWS_SSH::ALIAS_FILE
  File.open(filename, 'w') { |file| file.write(msg+cmd) }
  return File.exists?(filename)
end

#backupObject



66
67
68
69
70
71
# File 'lib/aws_ssh/setup.rb', line 66

def backup
  filename = "#{ENV['HOME']}/.ssh/config"
  if File.exists?(filename)
    FileUtils.copy_file(filename, "#{filename}.bk.#{Time.now.to_i}", true)
  end
end

#create_baseObject

move any current ssh config file to a base file



49
50
51
52
53
54
55
# File 'lib/aws_ssh/setup.rb', line 49

def create_base
  origin = "#{ENV['HOME']}/.ssh/config"
  dest = "#{ENV['HOME']}/.ssh/#{AWS_SSH::BASE_HOSTS_FILE}"
  if File.exists?(origin)
    FileUtils.mv(origin, dest)
  end
end

#envObject

all vars are required



13
14
15
# File 'lib/aws_ssh/setup.rb', line 13

def env
  return ( ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] && ENV['AWS_REGION'] && ENV['SHELL'] && ENV['HOME'])
end

#runObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/aws_ssh/setup.rb', line 73

def run
  self.backup

  if !self.env
    printf(AWS_SSH::FORMAT_FATAL, "Environment failed", "\u2620")
    return nil
  else
    printf(AWS_SSH::FORMAT_OK, "Environment vars", "\u2713")
  end

  self.profile
  if @profile.nil?
    printf(AWS_SSH::FORMAT_FATAL, "Shell profile failed", "\u2620")
    return nil
  else
    printf(AWS_SSH::FORMAT_OK, "Shell profile found", "\u2713")
  end

  if self.alias
    printf(AWS_SSH::FORMAT_OK, "Alias file created ", "\u2713")
  else
    printf(AWS_SSH::FORMAT_FATAL, "Alias file creation failed", "\u2620")
    return nil
  end

  self.create_base
  printf(AWS_SSH::FORMAT_OK, "Base SSH file created ", "\u2713")
  self.add_to_profile
  printf(AWS_SSH::FORMAT_OK, "Alias added to shell ", "\u2713")

  run = AWS_SSH::Run.new
  run.force
  run.merge
  printf(AWS_SSH::FORMAT_OK, "Completed first run", "\u2713")
  return true
end

#shellObject



17
18
19
20
21
22
# File 'lib/aws_ssh/setup.rb', line 17

def shell
  shell = ENV['SHELL']
  last = shell.rindex("/")+1
  len = shell.length-1
  return shell[last..len]
end