Beispiel #1
0
 public function __construct(Scalr_Net_Dns_Bind_Transports_Ssh2_AuthInfo $authInfo, $host, $port, $rndcPath, $zonesPath)
 {
     $this->ssh2Client = new Scalr_Net_Ssh2_Client();
     $this->logger = \Scalr::getContainer()->logger(__CLASS__);
     $this->rndcPath = $rndcPath;
     $this->zonesPath = $zonesPath;
     $this->host = $host;
     switch ($authInfo->getType()) {
         case Scalr_Net_Dns_Bind_Transports_Ssh2_AuthInfo::TYPE_PASSWORD:
             $this->ssh2Client->addPassword($authInfo->login, $authInfo->password);
             break;
         case Scalr_Net_Dns_Bind_Transports_Ssh2_AuthInfo::TYPE_PUBKEY:
             $this->ssh2Client->addPubkey($authInfo->login, $authInfo->pubKeyPath, $authInfo->privKeyPath, $authInfo->keyPassword);
             break;
     }
     try {
         $this->ssh2Client->connect($host, $port);
     } catch (Scalr_Net_Ssh2_Exception $e) {
         throw new Exception("Unable to initialize SSH2 Transport: {$e->getMessage()}");
     }
     // COunt initial number of zones
     $this->zonesCount = $this->rndcStatus();
     if (!$this->zonesCount) {
         throw new Exception(sprintf(_("Cannot fetch RNDC status on %s"), $host));
     }
 }
 /**
  * @return Scalr_Net_Ssh2_Client
  * Enter description here ...
  */
 public function GetSsh2Client()
 {
     $ssh2Client = new Scalr_Net_Ssh2_Client();
     switch ($this->platform) {
         case SERVER_PLATFORMS::RACKSPACE:
             $ssh2Client->addPassword('root', $this->GetProperty(RACKSPACE_SERVER_PROPERTIES::ADMIN_PASS));
             break;
         case SERVER_PLATFORMS::EC2:
             $userName = '******';
             // Temporary server for role builder
             if ($this->status == SERVER_STATUS::TEMPORARY) {
                 $keyName = 'SCALR-ROLESBUILDER';
             } else {
                 $keyName = "FARM-{$this->farmId}";
             }
             try {
                 $key = Scalr_Model::init(Scalr_Model::SSH_KEY)->loadGlobalByName($keyName, $this->GetProperty(EC2_SERVER_PROPERTIES::REGION), $this->envId);
                 if (!$key) {
                     throw new Exception(_("There is no SSH key for server: {$this->serverId}"));
                 }
             } catch (Exception $e) {
                 throw new Exception("Cannot init SshKey object: {$e->getMessage()}");
             }
             $priv_key_file = tempnam("/tmp", "AWSK");
             @file_put_contents($priv_key_file, $key->getPrivate());
             $pub_key_file = tempnam("/tmp", "AWSK");
             @file_put_contents($pub_key_file, $key->getPublic());
             $ssh2Client->addPubkey($userName, $pub_key_file, $priv_key_file);
             break;
     }
     return $ssh2Client;
 }
 /**
  * Upload S3cmd config file, AWS private key and certificate to instance aftre instance boot.
  * Also execute hostInit hooks from hooks/hostInit folder
  *
  * @param array $instanceinfo
  * @param string $local_ip
  * @param string $remote_ip
  * @param string $public_key
  */
 public function OnHostInit(HostInitEvent $event)
 {
     if ($event->DBServer->IsSupported("0.5")) {
         $this->Logger->info("Scalarizr instance. Skipping SSH observer...");
         return true;
     }
     if ($event->DBServer->platform != SERVER_PLATFORMS::EC2) {
         return true;
     }
     // Get farm info and client info from database;
     $DBFarm = DBFarm::LoadByID($this->FarmID);
     $DBRole = DBRole::loadById($event->DBServer->roleId);
     // Get Role info
     $ssh_port = $DBRole->getProperty(DBRole::PROPERTY_SSH_PORT) ? $DBRole->getProperty(DBRole::PROPERTY_SSH_PORT) : 22;
     // Generate s3cmd config file
     $s3cfg = CONFIG::$S3CFG_TEMPLATE;
     $s3cfg = str_replace("[access_key]", $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $s3cfg);
     $s3cfg = str_replace("[secret_key]", $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $s3cfg);
     $s3cfg = str_replace("\r\n", "\n", $s3cfg);
     // Prepare public key for SSH connection
     $pub_key_file = tempnam("/tmp", "AWSK");
     $res = file_put_contents($pub_key_file, $event->PublicKey);
     $this->Logger->debug("Creating temporary file for public key: {$res}");
     try {
         $key = Scalr_Model::init(Scalr_Model::SSH_KEY)->loadGlobalByFarmId($event->DBServer->farmId, $event->DBServer->GetFarmRoleObject()->CloudLocation);
         if (!$key) {
             throw new Exception(_("There is no SSH key for server: {$event->DBServer->serverId}"));
         }
     } catch (Exception $e) {
         throw new Exception("Cannot init SshKey object: {$e->getMessage()}");
     }
     // Prepare private key for SSH connection
     $priv_key_file = tempnam("/tmp", "AWSK");
     $res = file_put_contents($priv_key_file, $key->getPrivate());
     $this->Logger->debug("Creating temporary file for private key: {$res}");
     // Connect to SSH
     $SSH2 = new Scalr_Net_Ssh2_Client();
     $SSH2->addPubkey("root", $pub_key_file, $priv_key_file);
     if ($SSH2->connect($event->ExternalIP, $ssh_port)) {
         // Upload keys and s3 config to instance
         $res = $SSH2->sendFile("/etc/aws/keys/pk.pem", $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY), "w+", false);
         $res2 = $SSH2->sendFile("/etc/aws/keys/cert.pem", $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE), "w+", false);
         $res3 = $SSH2->sendFile("/etc/aws/keys/s3cmd.cfg", $s3cfg, "w+", false);
         // remove temporary files
         @unlink($pub_key_file);
         @unlink($priv_key_file);
     } else {
         // remove temporary files
         @unlink($pub_key_file);
         @unlink($priv_key_file);
         Logger::getLogger(LOG_CATEGORY::FARM)->warn(new FarmLogMessage($this->FarmID, "Cannot upload ec2 keys to '{$event->DBServer->serverId}' instance. Failed to connect to SSH '{$event->ExternalIP}:{$ssh_port}'"));
         throw new Exception("Cannot upload keys on '{$event->DBServer->serverId}'. Failed to connect to '{$event->ExternalIP}:{$ssh_port}'.");
     }
 }
Beispiel #4
0
 /**
  * @return Scalr_Net_Ssh2_Client
  * Enter description here ...
  */
 public function GetSsh2Client()
 {
     $ssh2Client = new Scalr_Net_Ssh2_Client();
     switch ($this->platform) {
         case SERVER_PLATFORMS::RACKSPACENG_UK:
         case SERVER_PLATFORMS::RACKSPACENG_US:
             $ssh2Client->addPassword('root', $this->GetProperty(OPENSTACK_SERVER_PROPERTIES::ADMIN_PASS));
             break;
         case SERVER_PLATFORMS::RACKSPACE:
             $ssh2Client->addPassword('root', $this->GetProperty(RACKSPACE_SERVER_PROPERTIES::ADMIN_PASS));
             break;
         case SERVER_PLATFORMS::GCE:
             $userName = '******';
             if ($this->status == SERVER_STATUS::TEMPORARY) {
                 $keyName = 'SCALR-ROLESBUILDER-' . SCALR_ID;
             } else {
                 $keyName = "FARM-{$this->farmId}-" . SCALR_ID;
             }
             try {
                 $key = (new SshKey())->loadGlobalByName($this->envId, SERVER_PLATFORMS::GCE, "", $keyName);
                 if (!$key) {
                     throw new Exception(_("There is no SSH key for server: {$this->serverId}"));
                 }
             } catch (Exception $e) {
                 throw new Exception("Cannot init SshKey object: {$e->getMessage()}");
             }
             $priv_key_file = tempnam("/tmp", "GCEPK");
             @file_put_contents($priv_key_file, $key->privateKey);
             $this->tmpFiles[] = $priv_key_file;
             $pub_key_file = tempnam("/tmp", "GCEK");
             @file_put_contents($pub_key_file, $key->publicKey);
             $this->tmpFiles[] = $pub_key_file;
             $ssh2Client->addPubkey($userName, $pub_key_file, $priv_key_file);
             break;
         case SERVER_PLATFORMS::IDCF:
         case SERVER_PLATFORMS::EC2:
             $userName = '******';
             $skipKeyValidation = false;
             // Temporary server for role builder
             $sshKey = new SshKey();
             if ($this->status == SERVER_STATUS::TEMPORARY) {
                 $keyName = "SCALR-ROLESBUILDER-" . SCALR_ID . "-{$this->envId}";
                 if (!$sshKey->loadGlobalByName($this->envId, $this->platform, $this->GetCloudLocation(), $keyName)) {
                     $keyName = "SCALR-ROLESBUILDER-" . SCALR_ID;
                 }
                 try {
                     $bundleTaskId = $this->GetProperty(\SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID);
                     $bundleTask = BundleTask::LoadById($bundleTaskId);
                     if ($bundleTask->osFamily == 'amazon') {
                         $userName = '******';
                     }
                 } catch (Exception $e) {
                 }
             } else {
                 $keyName = "FARM-{$this->farmId}-" . SCALR_ID;
                 $oldKeyName = "FARM-{$this->farmId}";
                 $key = $sshKey->loadGlobalByName($this->envId, $this->platform, $this->GetCloudLocation(), $oldKeyName);
                 if ($key) {
                     $keyName = $oldKeyName;
                     $skipKeyValidation = true;
                 }
             }
             if (!$skipKeyValidation) {
                 try {
                     $key = $sshKey->loadGlobalByName($this->envId, $this->platform, $this->GetCloudLocation(), $keyName);
                     if (!$key) {
                         throw new Exception(sprintf('Could not find SSH Key for server "%s" with name:"%s", cloud-location:"%s", platform:"%s", environment:"%d".', $this->serverId, $keyName, $this->GetCloudLocation(), $this->platform, $this->envId));
                     }
                 } catch (Exception $e) {
                     throw new Exception("Cannot init SshKey object: {$e->getMessage()}");
                 }
             }
             $priv_key_file = tempnam("/tmp", "AWSK");
             @file_put_contents($priv_key_file, $key->privateKey);
             $this->tmpFiles[] = $priv_key_file;
             $pub_key_file = tempnam("/tmp", "AWSK");
             $this->tmpFiles[] = $pub_key_file;
             $pubKey = $key->publicKey;
             if (!stristr($pubKey, $keyName)) {
                 $pubKey .= " {$keyName}";
             }
             @file_put_contents($pub_key_file, $pubKey);
             $ssh2Client->addPubkey($userName, $pub_key_file, $priv_key_file);
             break;
     }
     return $ssh2Client;
 }