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::GCE:
$userName = 'scalr';
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 = 'root';
$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 = 'ec2-user';
}
} 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;
}