Class: Baboon::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/baboon/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

initialize @param: Array @return: instance



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/baboon/cli.rb', line 25

def initialize *args
  super

  # Sync stdout for when communicating over ssh channel
  $stdout.sync ||= true

  # Store any errors here during the process so they can be printed to STDOUT
  @errors = []

  # Attempt to locate the configuration file in the project if it was not set.
  @configuration_file = Util.locate_baboon_configuration_file

  # Return if file not found
  if @configuration_file.nil?
    printf "#{BABOON_TITLE}\n"
    printf "  \033[22;31mError:\033[22;37m no configuration file is present anywhere in the application or at config/baboon.yml\n"
    printf "  \033[01;32mUsage:\033[22;37m rails g baboon:install\n"
    errors << "#initialize: failed to load a valid configuration file, please be sure a proper one is created at config/baboon.yml"
    @stop = true
    return
  end

  # Load the file now that we know its not nil
  @configuration = YAML.load_file(@configuration_file)

  # Double check that configuration is setup properly
  if @configuration['baboon'].has_key?('environments')
    @configuration['baboon']['environments'].map do |environment|
      hash = Hash[*environment]
      key  = hash.keys.first

      # Must have all defined keys filled out
      unless BABOON_ENVIRONMENT_SETTINGS.collect { |k| hash[key].has_key?(k) }.all?
        printf "#{BABOON_TITLE}\n"
        printf "  \033[22;31mError:\033[22;37m incorrect settings for the \"#{key}\" in config/baboon.yml\n"
        printf "  \033[01;32mUsage:\033[22;37m rails g baboon:install\n"
        errors << "#initialize: incorrect key:val [#{key}:#{k}] pair in config/baboon.yml"
        @stop = true
        return
      end
    end
  end
end

Instance Attribute Details

#configurationObject

@configuration: holds the default baboon configuration set in config file @configuration_file: the exact file location of baboon configuration



20
21
22
# File 'lib/baboon/cli.rb', line 20

def configuration
  @configuration
end

#configuration_fileObject

@configuration: holds the default baboon configuration set in config file @configuration_file: the exact file location of baboon configuration



20
21
22
# File 'lib/baboon/cli.rb', line 20

def configuration_file
  @configuration_file
end

#errorsObject

@configuration: holds the default baboon configuration set in config file @configuration_file: the exact file location of baboon configuration



20
21
22
# File 'lib/baboon/cli.rb', line 20

def errors
  @errors
end

#stopObject

@configuration: holds the default baboon configuration set in config file @configuration_file: the exact file location of baboon configuration



20
21
22
# File 'lib/baboon/cli.rb', line 20

def stop
  @stop
end

Instance Method Details

#deploy(environment) ⇒ Object



70
71
72
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/baboon/cli.rb', line 70

def deploy environment
  check_configuration(environment)

  raise InvalidConfigurationError.new("Your configuration is invalid, please check if you have misssss spelled something.") if @stop

  # Get the current config for this environment
  current_environment_configuration = @configuration['baboon']['environments'][environment]

  # Loop through the servers
  current_environment_configuration['servers'].each do |host|
    printf "Deploying[#{host}]\n"

    # TODO: add lib of different instruction sets for Play framework, nodejs, etc
    instructions = [
      "cd '#{current_environment_configuration['deploy_path']}' && git fetch",
      "cd '#{current_environment_configuration['deploy_path']}' && git checkout #{current_environment_configuration['branch']}",
      "cd '#{current_environment_configuration['deploy_path']}' && git merge origin/#{current_environment_configuration['branch']}",
      "cd '#{current_environment_configuration['deploy_path']}' && RAILS_ENV=#{current_environment_configuration['rails_env']} bundle install --deployment --without development test",
      "cd '#{current_environment_configuration['deploy_path']}' && RAILS_ENV=#{current_environment_configuration['rails_env']} bundle exec rake assets:precompile",
      "cd '#{current_environment_configuration['deploy_path']}' && RAILS_ENV=#{current_environment_configuration['rails_env']} bundle exec rake db:migrate"
    ]

    if current_environment_configuration.has_key?('restart_command') && !current_environment_configuration['restart_command'].nil?
      instructions << current_environment_configuration['restart_command']
    else
      instructions << "cd '#{current_environment_configuration['deploy_path']}' && touch tmp/restart.txt"
    end

    # Vars for connecting via ssh to the server
    credentials = {
      user: current_environment_configuration['deploy_user'],
      host: host
    }

    # Initialize the SSH class
    session = Net::SSH::Session.new(credentials[:host], credentials[:user], nil)

    # Open the session
    session.open

    # Set the rails_env session if var is present
    session.export_hash(
      'RAILS_ENV' => current_environment_configuration['rails_env'],
      'RACK_ENV'  => current_environment_configuration['rails_env']
    )

    # Check if user has added the callbacks block
    can_run_callbacks = current_environment_configuration.has_key?('callbacks')

    # Run pre instructions
    if can_run_callbacks
      pre_callbacks = current_environment_configuration['callbacks']['before_deploy']

      if !pre_callbacks.nil? && pre_callbacks.is_a?(Array) && pre_callbacks.count >= 1
        printf "[\033[36m#{host}\033[0m]: Running pre callbacks...\n"
        run_commands(host, session, pre_callbacks)
      end
    end

    # Execute commands
    run_commands(host, session, instructions)

    # Run post instructions
    if can_run_callbacks
      post_callbacks = current_environment_configuration['callbacks']['after_deploy']

      if !post_callbacks.nil? && post_callbacks.is_a?(Array) && post_callbacks.count >= 1
        printf "[\033[36m#{host}\033[0m]: Running post callbacks...\n"
        run_commands(host, session, post_callbacks)
      end
    end

    # Close and exit the session
    begin
      session.exit
    rescue => e
      # Here well want to write to stdout if --debug flag was passed
    end

    printf "[\033[36m#{host}\033[0m]: \033[32mComplete!\033[0m\n"
  end
end

#fetch(environment, file) ⇒ Object

Raises:

  • (InvalidHostError)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/baboon/cli.rb', line 154

def fetch environment, file
  current_configuration = @configuration['baboon']['environments'][environment]

  raise InvalidHostError.new("No host found in your configuration, please add one") if current_configuration['servers'].first.nil?

  host = current_configuration['servers'].first

  # Initialize the SSH class
  session = Net::SSH::Session.new(current_configuration['servers'].first, current_configuration['deploy_user'], nil)
  session.open

  file_path = file.gsub(/^\//, '')
  printf "[\033[36m#{host}\033[0m]: Fetching file '#{file_path}'\n"
  fetch_result = session.run("/bin/cat #{current_configuration['deploy_path']}/#{file_path}")
  session.exit

  printf "[\033[36m#{host}\033[0m]: Results below.\n"
  printf fetch_result.output + "\n"
  printf "[\033[36m#{host}\033[0m]: Complete.\n"
end

#refObject



176
177
178
179
180
# File 'lib/baboon/cli.rb', line 176

def ref
  current_configuration = @configuration['baboon']['environments'][environment]


end

#rollback(environment) ⇒ Object



183
184
185
# File 'lib/baboon/cli.rb', line 183

def rollback environment
  puts "This functionality has not been implemented yet, coming soon, I promise :)"
end

#versionObject



194
195
196
# File 'lib/baboon/cli.rb', line 194

def version
  puts Baboon::VERSION
end