lib/meroku/cli.rb in meroku-2.0.8 vs lib/meroku/cli.rb in meroku-2.0.9

- old
+ new

@@ -1,105 +1,104 @@ -require "meroku/cli/session" +require 'meroku/cli/help' +require 'meroku/cli/secrets' +require 'meroku/cli/session' module Meroku + # The CLI is used by end users and also maintainers module CLI + include Meroku::Aws::Ec2 - def help - <<~HEREDOC - Usage: meroku command subcommand - - Examples - - meroku signup # if you havent done already - - meroku create - - meroku keys:add - - meroku infrastrucuture spawn # Spawns server - - HEREDOC + def signup + email, pw = Meroku::Extensions.mgets(%i[email password]) + Meroku::Api::Request.post( + 'https://www.meroku.com/users.json', + user: { email: email, password: pw, password_confirmation: pw } + ) do |response| + puts "Signed up #{response['attributes']['email']}" + save_setting('apiusername', response['attributes']['apiusername']) + save_setting('apisecret', response['attributes']['apisecret']) + end + true end - def signup - print "Email: " - email = STDIN.gets - print "Password: " - password = STDIN.noecho(&:gets).chomp - print "\n" - url = "https://www.meroku.com/users.json" - response_json = RestClient.post url, {:user=>{:email => email, :password => password, :password_confirmation => password}}.to_json, timeout: 1, :content_type => :json, :accept => :json - if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 - puts "error: #{JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")}" - else - email = JSON.parse(response_json)["data"]["attributes"]["email"] - token = JSON.parse(response_json)["data"]["attributes"]["token"] - puts "Signed up #{email}" - dirname = File.dirname("#{Dir.home}/.meroku") - FileUtils.mkdir(dirname) if !File.directory?(dirname) - File.open("#{Dir.home}/.meroku/.token", 'w') { |file| file.write(token) } + def login + email, password = Meroku::Extensions.mgets(%i[email password]) + Meroku::Api::Request.post( + 'https://www.meroku.com/users/sign_in.json', + user: { email: email, password: password } + ) do |response| + puts "Signed in as #{response['attributes']['email']}" + save_setting('apiusername', response['attributes']['apiusername']) + save_setting('apisecret', response['attributes']['apisecret']) end + true end - def keys_add(session) - if !File.exist? "#{Dir.home}/.ssh/id_rsa.pub" - puts "error: File #{Dir.home}/.ssh/id_rsa.pub not found" - puts "You can use this command to generate a key:" - puts " ssh-keygen -t rsa" - return nil + def logout + [ + "#{Dir.home}/.meroku/.apiusername", + "#{Dir.home}/.meroku/.apisecret" + ].each do |f| + File.delete(f) if File.exist?(f) end - name = "id_rsa.pub" - data = `cat ~/.ssh/id_rsa.pub`.chomp - url = "https://www.meroku.com/publickeys.json" - response_json = RestClient.post url, {:publickey=>{:name => name, :data=>data}, :token=>session.token}.to_json, timeout: 1, :content_type => :json, :accept => :json - - if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 - puts "error: #{JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".")}" - else - name = JSON.parse(response_json)["data"]["attributes"]["name"] - puts "Added #{name}" - end + true end def create - url = "https://www.meroku.com/apps.json" - token = `cat ~/.meroku/.token`.chomp - response_json = RestClient.post url, {:app=>{:name => "unnamed"}, :token=>token}.to_json, timeout: 1, :content_type => :json, :accept => :json - - if JSON.parse(response_json)["errors"] && JSON.parse(response_json)["errors"].size > 0 - puts JSON.parse(response_json)["errors"].map{|x| x["detail"]}.join(".") - else - name = JSON.parse(response_json)["data"]["attributes"]["name"] - puts "Created #{name}, adding git remote" - puts "git remote remove meroku" - `git remote remove meroku 2>/dev/null` - puts "git remote add meroku #{token}@www.meroku.com:#{name}.git" - `git remote add meroku #{token}@www.meroku.com:#{name}.git` + Meroku::Api::Request.post( + 'https://www.meroku.com/apps.json', + app: { name: 'unnamed' }, apisecret: `cat ~/.meroku/.apisecret`.chomp + ) do |response| + puts "Created #{response['attributes']['name']}, adding git remote" + add_git_remote( + 'meroku', + "#{apiusername}@www.meroku.com:#{response['attributes']['name']}.git" + ) end + true end - def self.token_check - if !File.exist? "#{Dir.home}/.meroku/.token" - puts "error: Have you logged in yet?" - return nil + def apiusername + `cat ~/.meroku/.apiusername.chomp` + end + + def add_git_remote(name, uri) + puts "git remote remove #{name}" + `git remote remove #{name} 2>/dev/null` + puts "git remote add #{name} #{uri}" + `git remote add #{name} #{uri}` + end + + def keys_add + file = "#{Dir.home}/.ssh/id_rsa.pub" + apisecret = `cat ~/.meroku/.apisecret`.chomp + abort "error: #{file} not found" unless File.exist?(file) + Meroku::Api::Request.post( + 'https://www.meroku.com/publickeys.json', + publickey: { name: 'id_rsa.pub', data: `cat ~/.ssh/id_rsa.pub`.chomp }, + apisecret: apisecret + ) do |response| + puts "Added #{response['attributes']['name']}" end true end - def self.load_secrets - if !File.exist? "#{Dir.home}/.meroku/.secret" - puts "error: File #{Dir.home}/.meroku/.secret not found" - return nil - end - env_file = Dir.home+'/.meroku/meroku.env' - if !File.exist? env_file - secret=`cat ~/.meroku/.secret`.chomp - remote_env_file = "http://www.sam-we.com/dropbox/meroku-#{secret}/meroku.env" - File.write(env_file, Net::HTTP.get(URI.parse(remote_env_file))) - end - Dotenv.load(env_file) + def spawn + Meroku::CLI::Secrets.load(admin: true) + node = Meroku::Node.new + node.associate_address.configure_keys.add_sources.ubuntu_updates + node.install_packages.database_inits.git_clone.nginx_configs.start_rails + true end - end + def despawn + Meroku::CLI::Secrets.load(admin: true) + Meroku::Infrastructure.each_server(&:shutdown) + true + end - + def save_setting(name, value) + FileUtils.mkdir_p "#{Dir.home}/.meroku" + IO.write("#{Dir.home}/.meroku/.#{name}", value) + end + end end