class Object

Constants

TAR_BIN

Public Class Methods

add_rhlogin_config(rhlogin, uuid) click to toggle source

Add a new namespace to configs

# File lib/rhc-common.rb, line 1024
def self.add_rhlogin_config(rhlogin, uuid)
    config_path = RHC::Config.local_config_path
    f = open(File.expand_path(config_path), 'a')
    unless RHC::Config['default_rhlogin']
        f.puts("# Default rhlogin to use if none is specified")
        f.puts("default_rhlogin=#{rhlogin}")
        f.puts("")
    end
    f.close
end

Public Instance Methods

add_or_update_key(command, identifier, pub_key_file_path, rhlogin, password) click to toggle source

Public: Add or upload an ssh key

type - The String type RSA or DSS. command - The String value 'add' or 'update' identifier - The String value to identify the key pub_key_file_path - The String file path of the public key rhlogin - The String login to the broker password- The String password for the user

Examples

generate_ssh_key_ruby('add', 'newkeyname', '~/.ssh/id_rsa',
                      'mylogin', 'mypass')
# => /home/user/.ssh/id_rsa.pub

Returns nil on success or HTTP object on failure

# File lib/rhc-common.rb, line 1094
def add_or_update_key(command, identifier, pub_key_file_path, rhlogin, password)

  # Read user public ssh key
  if pub_key_file_path
    if File.readable?(pub_key_file_path)
      begin
        ssh_keyfile_contents = File.open(pub_key_file_path).gets.chomp.split(' ')
        ssh_key = ssh_keyfile_contents[1]
        ssh_key_type = ssh_keyfile_contents[0]
      rescue Exception => e
        puts "Invalid public keyfile format! Please specify a valid user public keyfile."
        exit 1
      end
    else
      puts "Unable to read user public keyfile #{pub_key_file_path}"
      exit 1
    end
  else # create key
    key_name = identifier
    puts "Generating ssh key pair for user '#{key_name}' in the dir '#{Dir.pwd}/'"
    # REMOVED in favor of generate_ssh_key_ruby: system("ssh-keygen -t rsa -f '#{key_name}'")
    ssh_pub_key_file = generate_ssh_key_ruby()
    ssh_keyfile_contents = File.open(ssh_pub_key_file).gets.chomp.split(' ')
    ssh_key = ssh_keyfile_contents[1]
    ssh_key_type = ssh_keyfile_contents[0]
  end

  data = {}
  data[:rhlogin] = rhlogin
  data[:key_name] = identifier
  data[:ssh] = ssh_key
  data[:action] = 'add-key'
  data[:key_type] = ssh_key_type

  if command == 'add'
    data[:action] = 'add-key'
  elsif command == 'update'
    data[:action] = 'update-key'
  end

  url = URI.parse("https://#{RHC::Config['libra_server']}/broker/ssh_keys")
  handle_key_mgmt_response(url, data, password)
end
ask_password() click to toggle source
# File lib/rhc-common.rb, line 967
def ask_password
  return ask("Password: ") { |q|
    q.echo = '*'
    q.whitespace = :chomp
  }
end
blank?() click to toggle source
# File lib/rhc/core_ext.rb, line 10
def blank?
  respond_to?(:empty?) ? empty? : !self
end
config() click to toggle source
# File lib/rhc-common.rb, line 963
def config
  return @opts_config ? @opts_config : @local_config
end
config_path() click to toggle source
# File lib/rhc-common.rb, line 959
def config_path
  return @opts_config_path ? @opts_config_path : @local_config_path
end
generate_ssh_key_ruby(type="RSA", bits = 1024, comment = "OpenShift-Key") click to toggle source

Public: Generate an SSH key and store it in ~/.ssh/id_rsa

type - The String type RSA or DSS. bits - The Integer value for number of bits. comment - The String comment for the key

Examples

generate_ssh_key_ruby()
# => /home/user/.ssh/id_rsa.pub

Returns nil on failure or public key location as a String on success

# File lib/rhc-common.rb, line 1151
def generate_ssh_key_ruby(type="RSA", bits = 1024, comment = "OpenShift-Key")
  key = RHC::Vendor::SSHKey.generate(:type => type,
                                     :bits => bits,
                                     :comment => comment)
  ssh_dir = "#{RHC::Config.home_dir}/.ssh"
  if File.exists?("#{ssh_dir}/id_rsa")
    puts "SSH key already exists: #{ssh_dir}/id_rsa.  Reusing..."
    return nil
  else
    unless File.exists?(ssh_dir)
      FileUtils.mkdir_p(ssh_dir)
      File.chmod(0700, ssh_dir)
    end
    File.open("#{ssh_dir}/id_rsa", 'w') {|f| f.write(key.private_key)}
    File.chmod(0600, "#{ssh_dir}/id_rsa")
    File.open("#{ssh_dir}/id_rsa.pub", 'w') {|f| f.write(key.ssh_public_key)}
  end
  "#{ssh_dir}/id_rsa.pub"
end
get_kfile(check_exists=true) click to toggle source
# File lib/rhc-common.rb, line 988
def get_kfile(check_exists=true)
  ssh_key_file = get_var('ssh_key_file')
  if ssh_key_file
    if (File.basename(ssh_key_file) == ssh_key_file)
      kfile = "#{ENV['HOME']}/.ssh/#{ssh_key_file}"
    else
      kfile = File.expand_path(ssh_key_file)
    end
  else
    kfile = "#{ENV['HOME']}/.ssh/id_rsa"
  end
  if check_exists && !File.exists?(kfile)
    if ssh_key_file
      puts "WARNING: Unable to find '#{kfile}' referenced in express.conf."
      kfile_not_found
    else
      kfile = "#{ENV['HOME']}/.ssh/id_rsa"
      if !File.exists?(kfile)
        puts "WARNING: Unable to find ssh key file."
        kfile_not_found
      end
    end
  end
  return kfile
end
get_kpfile(kfile, check_exists=true) click to toggle source
# File lib/rhc-common.rb, line 1014
def get_kpfile(kfile, check_exists=true)
  kpfile = kfile + '.pub'
  if check_exists && !File.exists?(kpfile)
    puts "WARNING: Unable to find '#{kpfile}'"
    kfile_not_found
  end
  return kpfile
end
get_var(key) click to toggle source

Public: legacy convinience function for getting config keys

# File lib/rhc-common.rb, line 1203
def get_var(key)
  RHC::Config[key]
end
handle_key_mgmt_response(url, data, password) click to toggle source

Public: Handle response message when updating keys

url - The Object URI::HTTPS data - The Hash representation of the data response password - The String password for the user

Examples

handle_key_mgmt_response(
                URI.parse('https://openshift.redhat.com/broker/ssh_keys'),
                {
                  :rhlogin=>"rhnlogin@example.com",
                  :key_name=>"default",
                  :action=>"update-key",
                  :ssh=>"AAAAB3NzaC1yc2EAAAADAQABAAAAgQCrXG5c.....",
                  :key_type=>"ssh-rsa"},
                'mypass')
# => nil

Returns nil on Success and RHC::http object on failure

# File lib/rhc-common.rb, line 1055
def handle_key_mgmt_response(url, data, password)
  RHC::print_post_data(data)
  json_data = RHC::generate_json(data)

  response = RHC::http_post(RHC::Config.default_proxy, url, json_data, password)

  if response.code == '200'
    begin
      json_resp = RHC::json_decode(response.body)
      RHC::update_server_api_v(json_resp)
      RHC::print_response_success(json_resp)
      puts "Success"
      return
    rescue RHC::JsonError
      RHC::print_response_err(response)
    end
  else
    RHC::print_response_err(response)
  end
  puts "Failure"
  return response
end
kfile_not_found() click to toggle source
# File lib/rhc-common.rb, line 974
def kfile_not_found
  puts "Your SSH keys are created either by running ssh-keygen (password optional)
or by having the 'rhc domain create' command do it for you.  If you created
them on your own (or want to use an existing keypair), be sure to paste
your public key into the express console at http://www.openshift.com.
The client tools use the value of 'ssh_key_file' in express.conf to find
your key followed by the defaults of id_rsa[.pub] and then
id_rsa[.pub].
"

#exit 212
end
present?() click to toggle source
# File lib/rhc/core_ext.rb, line 7
def present?
  !blank?
end
ssh_ruby(host, username, command) click to toggle source

Public: Run ssh command on remote host

host - The String of the remote hostname to ssh to. username - The String username of the remote user to ssh as. command - The String command to run on the remote host.

Examples

ssh_ruby('myapp-t.rhcloud.com',
          '109745632b514e9590aa802ec015b074',
          'rhcsh tail -f $OPENSHIFT_LOG_DIR/*"')
# => true

Returns true on success

# File lib/rhc-common.rb, line 1185
def ssh_ruby(host, username, command)
  Net::SSH.start(host, username) do |session|
    session.open_channel do |channel|
      channel.request_pty do |ch, success|
        puts "pty could not be obtained" unless success
      end

      channel.on_data do |ch, data|
        #puts "[#{file}] -> #{data}"
        puts data
      end
      channel.exec command
    end
    session.loop
  end
end
to_json(options=nil) click to toggle source
# File lib/rhc/core_ext.rb, line 16
def to_json(options=nil)
  RHC::Json.encode(self)
end