Permalink
Browse files

SD CLI initial commit

  • Loading branch information...
0 parents commit cd331c1783ff0bb405927037a557470963d4105f Roman Kononov committed Dec 28, 2010
No changes.
@@ -0,0 +1,19 @@
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gemspec|
+ gemspec.name = "simple_deployr"
+ gemspec.summary = "Appoxy simple_deployr Gem"
+ gemspec.description = "Appoxy simple_deployr Gem"
+ gemspec.email = "roman@appoxy.com"
+ gemspec.homepage = "http://www.appoxy.com"
+ gemspec.files = FileList["[A-Z]*", "lib/**/*","bin/sd"]
+ gemspec.authors = ["Roman Kononov"]
+ gemspec.executables = "sd"
+ gemspec.add_dependency 'aws'
+ gemspec.add_dependency 'zip'
+ gemspec.add_dependency 'appoxy_api'
+ gemspec.add_development_dependency "rake"
+ end
+rescue LoadError
+ puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gems.github.com"
+end
@@ -0,0 +1,6 @@
+
+:major: 0
+:minor: 1
+:patch: 0
+
+
13 bin/sd
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
+$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
+
+require 'simple_deployr'
+require 'simple_deployr/command'
+
+args = ARGV.dup
+ARGV.clear
+command = args.shift.strip rescue 'help'
+
+SimpleDeployr::Command.run(command, args)
@@ -0,0 +1,16 @@
+require 'simple_deployr/client'
+require 'simple_deployr/config'
+require 'appoxy_api'
+module SimpleDeployr
+
+ class << self
+ attr_accessor :config,
+ :service
+
+ def configure()
+ SimpleDeployr.config ||= Config.new
+ yield(config)
+ SimpleDeployr.service = Service.new(config.access_key, config.secret_key, :config=>config)
+ end
+ end
+end
@@ -0,0 +1,76 @@
+require 'aws'
+require 'appoxy_api'
+module SimpleDeployr
+
+ class Service < Appoxy::Api::Client
+
+ attr_accessor :config
+
+
+ def initialize(config, options={})
+ puts 'Starting SimpleDeployr...'
+ self.config = config
+ super("http://www.simpledeployr.com/api/", config.access_key, config.secret_key, options)
+ self.host = self.config.host if self.config && self.config.host
+ end
+
+
+ def project_info(project_id)
+ project = get("project/#{project_id}")
+ end
+
+
+ def create_project(name, filename, aws_access_key, aws_secret_key)
+ data = {"name"=>name, "scm_url"=>filename, "aws_access_key"=>aws_access_key, "aws_secret_key"=>aws_secret_key}
+ ret = post("projects", data)
+ end
+
+
+ def launch_server(project_id)
+ data = {"launch_in_worker"=>"true"}
+ ret = get("projects/#{project_id}/launch_server", data)
+ end
+
+
+ def servers_list(project_id)
+ servers = get("projects/#{project_id}/servers")
+ end
+
+
+ def databases_list(project_id)
+ databases = get("projects/#{project_id}/databases")
+ end
+
+
+ def load_balancer(project_id)
+ load_balancer = get("projects/#{project_id}/load_balancer")
+ end
+
+
+ def projects_list()
+ projects = get("projects")
+ end
+
+
+ def deploy_code(project_id, params={})
+ data = params
+ p "DATA is" + data.to_s
+ ret = get("projects/#{project_id}/deploy_code", data)
+ end
+
+
+ def server_status(server_id)
+ status = get("servers/#{server_id}")
+ end
+
+
+ def reboot_server(server_id)
+ get("servers/#{server_id}/reboot")
+ end
+
+
+ def terminate_server(server_id)
+ delete("servers/#{server_id}")
+ end
+ end
+end
@@ -0,0 +1,31 @@
+require 'simple_deployr/commands/base'
+Dir["#{File.dirname(__FILE__)}/commands/*.rb"].each { |c| require c }
+module SimpleDeployr
+ module Command
+ class << self
+ def run(command, args)
+ config = Config.new()
+ config.read()
+ begin
+ service = SimpleDeployr::Service.new(config) #authorization should be there
+ klass, method = parse(command)
+ runner = klass.new(args, service)
+ runner.send(method)
+ rescue RestClient::RequestTimeout
+ p "API request timed out."
+ rescue NameError,NoMethodError =>ex
+ p "Invalid command please type 'sd' for help"
+ p ex.inspect
+ end
+ end
+
+
+ def parse(command)
+ begin
+ parts = command.split(".")
+ return eval("SimpleDeployr::Command::#{parts[0].capitalize}"), parts[1] ? parts[1].to_sym : :main
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,46 @@
+module SimpleDeployr::Command
+ class CommandsBase
+ def initialize(args, service)
+ @args = args
+ @service = service
+ end
+
+
+ private
+ def upload(file)
+ f = File.open(file, "r")
+ data = ''
+ f.each_line do |line|
+ data += line
+ end
+ @service.config.s3_bucket.put(File.basename(file), data)
+ end
+
+
+ def package(path)
+ require 'zip/zip'
+ path.sub!(%r[/$], '')
+ archive = File.join(path, File.basename(path))+'.zip'
+ FileUtils.rm archive, :force=>true
+ Zip::ZipFile.open(archive, 'w') do |zipfile|
+ Dir["#{path}/**/**"].each do |file|
+ zipfile.add(file.sub(path+'/', ''), file)
+ end
+ end
+ return archive
+ end
+
+
+ def get_project
+ @pr_id = @args.empty? ? @service.config.project_id : @args[0]
+ raise "You should define project id" unless @pr_id
+ end
+
+
+ def get_server
+ @server_id = @args ? @args[0] : nil
+ raise "You should define server id" unless @server_id
+ end
+
+ end
+end
@@ -0,0 +1,27 @@
+module SimpleDeployr::Command
+ class Help < CommandsBase
+ def main
+ help = <<EOF
+Available commands
+
+sd info - account info
+
+sd projects - projects list for current user
+sd project pr_id - project info
+sd project.create - create a project from with source code from current folder
+sd project.servers pr_id - servers list for project pr_id
+sd project.launch_server pr_id - launch new server in project pr_id
+sd project.databases pr_id - list of databases in project pr_id
+sd project.load_balancer pr_id - load balancer in project pr_id
+sd project.deploy_folder pr_id - deploy current folder content in all servers of project pr_id
+sd project.deploy pr_id - deploy latest code to all servers of project pr_id
+
+sd server server_id - server status of server_id server
+sd server.reboot server_id - reboot server server_id
+sd server.terminate server_id - terminate server server_id
+EOF
+ # help.each_line{|l| p l}
+ print help
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module SimpleDeployr::Command
+
+ class Info < CommandsBase
+
+ def main
+ puts "Account info"
+ p "Access_key " + @service.config.access_key
+ p "Secret_key " + @service.config.secret_key
+ p "Aws_access_key " + @service.config.aws_access_key
+ p "Aws_secret_key " + @service.config.aws_secret_key
+ p "Current project id " + (@service.config.project_id || "Undefined")
+ end
+ end
+end
@@ -0,0 +1,84 @@
+module SimpleDeployr::Command
+
+ class Project < CommandsBase
+
+ def main
+ get_project
+ project = @service.project_info(@pr_id)
+ p "Project info"
+ project.each_pair do |k, v|
+ p "#{k}:#{v}"
+ end
+ end
+
+
+ def create
+ puts "Packaging local folder"
+ file = package(Dir.getwd)
+ p "Packing done"
+ p "Uploading"
+ upload(file)
+ p "creating project"
+ pr_id = @service.create_project(File.basename(file, '.*') + "_project", File.basename(file), @service.config.aws_access_key, @service.config.aws_secret_key)
+ @service.config.project_id = pr_id
+ @service.config.save_config
+ p "Project created, project id saved as default"
+# p "launching server " + pr_id
+# @service.launch_server(pr_id)
+# p "server launched"
+ end
+
+
+ def servers
+ get_project
+ servers = @service.servers_list(@pr_id)
+ if servers.size >0
+ servers.each do |s|
+ p "ID:" + s["id"] + "; status:" + s["status"]
+ end
+ else
+ p "No active servers"
+
+ end
+ end
+
+
+ def launch_server
+ get_project
+ @service.launch_server(@pr_id)
+ end
+
+
+ def databases
+ get_project
+ databases = @service.databases_list(@pr_id)
+ p databases.size >0 ? databases : "No active databases"
+
+ end
+
+
+ def load_balancer
+ get_project
+ lb = @service.load_balancer(@pr_id)
+ p lb ? lb : "No load balancer"
+ end
+
+
+ def deploy_folder
+ get_project
+ file = package(Dir.getwd)
+ upload(file)
+ @service.deploy_code(pr_id, "scm_url"=> File.basename(file), "scm_type"=>"s3")
+ p "Code deployed"
+ end
+
+
+ def deploy
+ get_project
+ @service.deploy_code(@pr_id)
+ end
+
+
+ end
+end
+
@@ -0,0 +1,13 @@
+module SimpleDeployr::Command
+
+ class Projects < CommandsBase
+
+ def main
+ projects = @service.projects_list()
+ projects.each do |pr|
+ p "Name:" + pr["name"] + "; id:" + pr["id"] + "; active servers:"+pr["num_servers"].to_s
+ end
+ end
+ end
+end
+
@@ -0,0 +1,22 @@
+module SimpleDeployr::Command
+
+ class Server < CommandsBase
+ def main
+ get_server
+ p @service.server_status(@server_id)
+ end
+
+
+ def reboot
+ get_server
+ p @service.reboot_server(@server_id)
+ end
+
+
+ def terminate
+ get_server
+ p @service.terminate_server(@server_id)
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit cd331c1

Please sign in to comment.