lib/simple_deploy/stack.rb in simple_deploy-0.2.8 vs lib/simple_deploy/stack.rb in simple_deploy-0.3.0
- old
+ new
@@ -1,6 +1,7 @@
require 'stackster'
+require 'simple_deploy/stack/deployment'
require 'simple_deploy/stack/stack_reader'
require 'simple_deploy/stack/stack_lister'
require 'simple_deploy/stack/stack_attribute_formater'
module SimpleDeploy
@@ -8,48 +9,66 @@
def initialize(args)
@environment = args[:environment]
@name = args[:name]
@config = Config.new :logger => args[:logger]
+ @logger = @config.logger
end
- def self.list(args)
- StackLister.new(:config => args[:config]).all
- end
-
def create(args)
- saf = StackAttributeFormater.new(:attributes => args[:attributes],
- :config => @config,
- :environment => @environment)
- stack.create :attributes => saf.updated_attributes,
+ stack.create :attributes => stack_attribute_formater.updated_attributes(args[:attributes]),
:template => args[:template]
end
def update(args)
- saf = StackAttributeFormater.new(:attributes => args[:attributes],
- :config => @config,
- :environment => @environment)
- stack.update :attributes => saf.updated_attributes
+ stack.update :attributes => stack_attribute_formater.updated_attributes(args[:attributes])
end
- def deploy
- deployment = Deployment.new :config => @config,
- :environment => @environment,
- :instances => instances,
- :attributes => attributes,
- :ssh_gateway => stack.attributes['ssh_gateway'],
- :ssh_user => ENV['SIMPLE_DEPLOY_SSH_USER'],
- :ssh_key => ENV['SIMPLE_DEPLOY_SSH_KEY']
+ # To Do: Abstract deployment into it's own class
+ # Pass in required stack objects for attribut mgmt
+ def deploy(force = false)
+ @logger.info "Checking deployment status."
+ if deployment_in_progress?
+ @logger.info "Deployment in progress."
+ @logger.info "Started by #{attributes['deployment_user']}@#{attributes['deployment_datetime']}."
+ if force
+ clear_deployment_status
+ else
+ @logger.error "Exiting due to existing deployment."
+ @logger.error "Use -f to override."
+ exit 1
+ end
+ else
+ @logger.info "No other deployments in progress."
+ end
+ set_deployment_in_progress
deployment.execute
+ clear_deployment_status
end
+ def deployment_in_progress?
+ attributes['deployment_in_progress'] == 'true'
+ end
+
+ def set_deployment_in_progress
+ @logger.info "Setting deployment in progress by #{ssh_user}."
+ stack.update :attributes => [ { 'deployment_in_progress' => 'true',
+ 'deployment_user' => ssh_user,
+ 'deployment_datetime' => Time.now.to_s } ]
+ end
+
+ def clear_deployment_status
+ @logger.info "Clearing deployment status."
+ stack.update :attributes => [ { 'deployment_in_progress' => '' } ]
+ end
+
def destroy
stack.destroy
end
- def events
- stack.events
+ def events(limit)
+ stack.events limit
end
def outputs
stack.outputs
end
@@ -86,10 +105,32 @@
def stack
@stack ||= Stackster::Stack.new :environment => @environment,
:name => @name,
:config => @config.environment(@environment),
- :logger => @config.logger
+ :logger => @logger
+ end
+
+ def stack_attribute_formater
+ @saf ||= StackAttributeFormater.new :config => @config,
+ :environment => @environment
+ end
+
+ def deployment
+ @deployment ||= Stack::Deployment.new :config => @config,
+ :environment => @environment,
+ :instances => instances,
+ :attributes => attributes,
+ :ssh_user => ssh_user,
+ :ssh_key => ssh_key
+ end
+
+ def ssh_key
+ ENV['SIMPLE_DEPLOY_SSH_KEY'] ||= "#{ENV['HOME']}/.ssh/id_rsa"
+ end
+
+ def ssh_user
+ ENV['SIMPLE_DEPLOY_SSH_USER'] ||= ENV['USER']
end
end
end