Class: Morpheus::Cli::Deploys

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/deploys.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeDeploys

Returns a new instance of Deploys.



14
15
16
# File 'lib/morpheus/cli/deploys.rb', line 14

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#connect(opts) ⇒ Object



18
19
20
21
22
# File 'lib/morpheus/cli/deploys.rb', line 18

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @instances_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instances
  @deploy_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).deploy
end

#deploy(args) ⇒ Object



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
68
69
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
# File 'lib/morpheus/cli/deploys.rb', line 28

def deploy(args)
  options={}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = "Usage: morpheus deploy [environment]"
    build_common_options(opts, options, [])
    opts.footer = "Deploy to an environment using the morpheus.yml file, located in the working directory."
    # "todo: document me better!"
  end
  optparse.parse!(args)
  connect(options)
  environment = 'production' # yikes!
  if args.count > 0
    environment = args[0]
  end
  if load_deploy_file().nil?
    puts "Morpheus Deploy File `morpheus.yml` not detected. Please create one and try again."
    return
  end

  deploy_args = merged_deploy_args(environment)
  if deploy_args['name'].nil?
    puts "Instance not specified. Please specify the instance name and try again."
    return
  end

  instance_results = @instances_interface.get(name: deploy_args['name'])
  if instance_results['instances'].empty?
    puts "Instance not found by name #{args[0]}"
    return
  end
  instance = instance_results['instances'][0]
  instance_id = instance['id']
  print_h1 "Morpheus Deployment"
  if !deploy_args['script'].nil?
    print cyan, bold, "  - Executing Pre Deploy Script...", reset, "\n"

    if !system(deploy_args['script'])
      puts "Error executing pre script..."
      return
    end
  end
  # Create a new deployment record
  deploy_result = @deploy_interface.create(instance_id)
  app_deploy = deploy_result['appDeploy']
  deployment_id = app_deploy['id']

  # Upload Files
  print "\n",cyan, bold, "Uploading Files...", reset, "\n"
  current_working_dir = Dir.pwd
  deploy_args['files'].each do |fmap|
    Dir.chdir(fmap['path'] || current_working_dir)
    files = Dir.glob(fmap['pattern'] || '**/*')
    files.each do |file|
      if File.file?(file)
        print cyan,bold, "  - Uploading #{file} ...", reset, "\n"
        destination = file.split("/")[0..-2].join("/")
        @deploy_interface.upload_file(deployment_id,file,destination)
      end
    end
  end
  print cyan, bold, "Upload Complete!", reset, "\n"
  Dir.chdir(current_working_dir)

  if !deploy_args['post_script'].nil?
    print cyan, bold, "Executing Post Script...", reset, "\n"
    if !system(deploy_args['post_script'])
      puts "Error executing post script..."
      return
    end
  end

  deploy_payload = {}
  if deploy_args['env']
    evars = []
    deploy_args['env'].each_pair do |key, value|
      evars << {name: key, value: value, export: false}
    end
    payload = {envs: evars}
    @instances_interface.create_env(instance_id, payload)
    @instances_interface.restart(instance_id)
  end
  if deploy_args['options']
    deploy_payload = {
      appDeploy: {
        config: deploy_args['options']
      }
    }
  end

  print cyan, bold, "Deploying to Servers...", reset, "\n"
  @deploy_interface.deploy(deployment_id,deploy_payload)
  print cyan, bold, "Deploy Successful!", reset, "\n"
end

#handle(args) ⇒ Object



24
25
26
# File 'lib/morpheus/cli/deploys.rb', line 24

def handle(args)
  deploy(args)
end

#list(args) ⇒ Object



122
123
# File 'lib/morpheus/cli/deploys.rb', line 122

def list(args)
end

#load_deploy_fileObject

Loads a morpheus.yml file from within the current working directory. This file contains information necessary in the project to perform a deployment via the cli

Example File Attributes

  • script - The initial script to run before uploading files

  • name - The instance name we are deploying to (can be overridden in CLI)

  • remote - Optional remote appliance name we are connecting to

  • files - List of file patterns to use for uploading files and their target destination

  • options - Map of deployment options depending on deployment type

  • post_script - A post operation script to be run on the local machine

  • stage_deploy - If set to true the deploy will only be staged and not actually run

NOTE: It is also possible to nest these properties in an “environments” map to override based on a passed environment deploy name



142
143
144
145
146
147
148
149
150
# File 'lib/morpheus/cli/deploys.rb', line 142

def load_deploy_file
  if !File.exist? "morpheus.yml"
    puts "No morpheus.yml file detected in the current directory. Nothing to do."
    return nil
  end

  @deploy_file = YAML.load_file("morpheus.yml")
  return @deploy_file
end

#merged_deploy_args(environment) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/morpheus/cli/deploys.rb', line 152

def merged_deploy_args(environment)
  environment = environment || production

  deploy_args = @deploy_file.reject { |key,value| key == 'environment'}
  if !@deploy_file['environment'].nil? && !@deploy_file['environment'][environment].nil?
    deploy_args = deploy_args.merge(@deploy_file['environment'][environment])
  end
  return deploy_args
end

#rollback(args) ⇒ Object



125
126
# File 'lib/morpheus/cli/deploys.rb', line 125

def rollback(args)
end