Class: OpsDeploy::CLI
- Inherits:
-
Object
- Object
- OpsDeploy::CLI
- Defined in:
- lib/ops_deploy/cli.rb
Defined Under Namespace
Class Method Summary collapse
Instance Method Summary collapse
- #check_instances(stack_id_name_or_object, via_proxy = false) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #post_latest_commit(stack_id_name_or_object) ⇒ Object
- #start_deployment(stack_id_name_or_object, application_id = nil, migrate = false) ⇒ Object
- #start_server ⇒ Object
- #wait_for_deployments(stack_id_name_or_object, via_proxy = false) ⇒ Object
- #with_slack(slack_options = nil) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 |
# File 'lib/ops_deploy/cli.rb', line 3 def initialize config = { region: OpsDeploy::CLI.argument('aws-region', 'AWS_REGION', true) } profile = OpsDeploy::CLI.argument('aws-profile', 'AWS_PROFILE') config[:credentials] = Aws::SharedCredentials.new(profile_name: profile) if profile aws_access_key_id = OpsDeploy::CLI.argument('aws-access-key-id', 'AWS_ACCESS_KEY_ID') aws_secret_access_key = OpsDeploy::CLI.argument('aws-secret-access-key', 'AWS_SECRET_ACCESS_KEY') if aws_access_key_id && aws_secret_access_key config[:access_key_id] = aws_access_key_id config[:secret_access_key] = aws_secret_access_key end @main = OpsDeploy.new(config) @stacks = {} github_token = OpsDeploy::CLI.argument('github-token', 'GITHUB_TOKEN') auto_inactive = OpsDeploy::CLI.argument('github-deployment-auto-inactive') auto_inactive = [nil, "", "1", "true", "TRUE"].include?(auto_inactive) @github = OpsDeploy::CLI::GitHub.new(github_token, { owner: OpsDeploy::CLI.argument('github-deployment-owner'), repo: OpsDeploy::CLI.argument('github-deployment-repo'), deployment_id: OpsDeploy::CLI.argument('github-deployment-deployment-id'), options: { log_url: OpsDeploy::CLI.argument('github-deployment-log-url'), description: OpsDeploy::CLI.argument('github-deployment-description'), environment_url: OpsDeploy::CLI.argument('github-deployment-environment-url'), auto_inactive: auto_inactive } }) @notifier = OpsDeploy::CLI::Notifier.new(slack: { webhook_url: OpsDeploy::CLI.argument('slack-webhook-url', 'SLACK_WEBHOOK_URL'), username: OpsDeploy::CLI.argument('slack-username', 'SLACK_USERNAME'), channel: OpsDeploy::CLI.argument('slack-channel', 'SLACK_CHANNEL'), notify_user: OpsDeploy::CLI.argument('slack-notify-user', 'SLACK_NOTIFY_USER') }) @notification_messages = { info: [], success: [], failure: [] } @notification_success = false @notification_failure = false end |
Class Method Details
.argument(argv_name, env_name = nil, required = false) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ops_deploy/cli.rb', line 202 def self.argument(argv_name, env_name = nil, required = false) value = nil value = ENV[env_name] if env_name if value.nil? value = ARGV.include?(argv_name) ? true : nil return value if value ARGV.each do |arg| if arg.start_with?("--#{argv_name}=") value = arg.split("--#{argv_name}=").last end end end if required && value.nil? = env_name ? "the environment variable #{env_name} or " : '' error = "Argument '#{argv_name}' unspecified." suggestion = "Please set #{}the argument --#{argv_name}=<value>" fail StandardError.new("#{error} #{suggestion}") end value end |
Instance Method Details
#check_instances(stack_id_name_or_object, via_proxy = false) ⇒ Object
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 152 153 |
# File 'lib/ops_deploy/cli.rb', line 120 def check_instances(stack_id_name_or_object, via_proxy = false) stack = find_stack(stack_id_name_or_object) if via_proxy Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true) Pusher.trigger('OpsDeploy', 'check_instances', stack: stack.stack_id, slack: @notifier.[:slack]) else @main.instances_check_callback = proc { |instance, response, error| puts if error.nil? && response.code == 200 success_msg('Response from', "#{instance.hostname.green}:", '200 OK'.green) elsif error.nil? failure_msg('Response from', "#{instance.hostname.red}:", "#{response.code}".red) else failure_msg('Error checking', "#{instance.hostname.red}:", error.to_s) end } waiter = @main.check_instances(stack) if waiter step_msg("Checking instances' HTTP response...") print '.'.blue print '.'.blue until waiter.join(1) info_msg('Response check finished') else info_msg('No online instances on stack', "'#{stack_id_name_or_object.blue}'") end send_notification(stack) end end |
#post_latest_commit(stack_id_name_or_object) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/ops_deploy/cli.rb', line 53 def post_latest_commit(stack_id_name_or_object) stack = find_stack(stack_id_name_or_object) latest_commit_description = `git log -1 --pretty=medium` step_msg('Latest commit: ', latest_commit_description) send_notification(stack) end |
#start_deployment(stack_id_name_or_object, application_id = nil, migrate = false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ops_deploy/cli.rb', line 60 def start_deployment(stack_id_name_or_object, application_id = nil, migrate = false) stack = find_stack(stack_id_name_or_object) step_msg('Starting deployment on stack', "'#{stack.name.blue}'...") if @main.start_deployment(stack, application_id, migrate) success_msg('Deployment started on stack', "'#{stack.name.green}'") send_notification(stack) true else failure_msg("Couldn't start deployment on stack", "'#{stack.name.red}'") send_notification(stack) false end end |
#start_server ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ops_deploy/cli.rb', line 155 def start_server pusher_comp = URI.parse(OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true)) PusherClient.logger.level = Logger::ERROR socket = PusherClient::Socket.new(pusher_comp.user, secure: true) socket.subscribe('OpsDeploy') socket['OpsDeploy'].bind('check_instances') do |data| begin info = data info = symbolize_keys(JSON.parse(data)) if data.is_a?(String) with_slack(info[:slack]) do check_instances(info[:stack]) end rescue StandardError => e puts e end end socket['OpsDeploy'].bind('wait_for_deployments') do |data| begin info = data info = symbolize_keys(JSON.parse(data)) if data.is_a?(String) @github.deployment_info = info[:github] with_slack(info[:slack]) do wait_for_deployments(info[:stack]) end rescue StandardError => e puts e end end info_msg("Started OpsDeploy server #{OpsDeploy::VERSION}") socket.connect end |
#wait_for_deployments(stack_id_name_or_object, via_proxy = false) ⇒ Object
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 |
# File 'lib/ops_deploy/cli.rb', line 78 def wait_for_deployments(stack_id_name_or_object, via_proxy = false) stack = find_stack(stack_id_name_or_object) if via_proxy Pusher.url = OpsDeploy::CLI.argument('pusher-url', 'PUSHER_URL', true) Pusher.trigger('OpsDeploy', 'wait_for_deployments', stack: stack.stack_id, slack: @notifier.[:slack], github: @github.deployment_info) else step_msg('Checking deployments...') github = @github.dup @main.deployments_callback = proc { |deployment| puts if (deployment.status == 'successful') success_msg('Deployment', 'OK'.green.bold, deployment.duration ? "(#{deployment.duration}s)" : '') github.create_deployment_status("success") else failure_msg('Deployment', 'Failed'.red.bold, deployment.duration ? "(#{deployment.duration}s)" : '') github.create_deployment_status("error") end } waiter = @main.wait_for_deployments(stack) if waiter step_msg('Waiting for deployments to finish...') print '.'.blue print '.'.blue until waiter.join(1) info_msg('Deployments finished') else info_msg('No running deployments on stack', "'#{stack_id_name_or_object.blue}'") end send_notification(stack) end end |