Skip to content

Commit 905b7d5

Browse files
committed
Prepare code for ability to reuse the same instance_unique_id when
migrating instance.
1 parent 4930c32 commit 905b7d5

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

class/sellyoursaasutils.class.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,7 +4306,7 @@ public function sellyoursaasRemoteAction($remoteaction, $object, $appusername =
43064306
$sldAndSubdomain = $tmp[0];
43074307
$domainname = $tmp[1];
43084308

4309-
4309+
43104310
// Now loop on each lines qualified for action and call remote URL
43114311
foreach ($listoflinesqualified as $tmparrayoflinesqualified) {
43124312
$tmpobject = $tmparrayoflinesqualified['tmpobject'];
@@ -4331,7 +4331,7 @@ public function sellyoursaasRemoteAction($remoteaction, $object, $appusername =
43314331
}
43324332

43334333
$ispaidinstance = sellyoursaasIsPaidInstance($contract);
4334-
4334+
43354335
if (! empty($contract->array_options['options_deployment_host'])) {
43364336
$serverdeployment = $contract->array_options['options_deployment_host'];
43374337
} else {
@@ -4390,7 +4390,7 @@ public function sellyoursaasRemoteAction($remoteaction, $object, $appusername =
43904390
$generateddbprefix = (empty($contract->array_options['options_prefix_db']) ? 'llx_' : $contract->array_options['options_prefix_db']);
43914391
$generatedunixhostname = $contract->array_options['options_hostname_os'];
43924392
$generateddbhostname = $contract->array_options['options_hostname_db'];
4393-
$generateduniquekey = getRandomPassword(true);
4393+
$generateduniquekey = empty($contract->array_options['options_instance_unique_id']) ? getRandomPassword(true) : $contract->array_options['options_instance_unique_id'];
43944394

43954395
$sshaccesstype = (empty($contract->array_options['options_sshaccesstype']) ? 0 : $contract->array_options['options_sshaccesstype']);
43964396
$customurl = (empty($contract->array_options['options_custom_url']) ? '' : $contract->array_options['options_custom_url']);
@@ -4487,14 +4487,14 @@ public function sellyoursaasRemoteAction($remoteaction, $object, $appusername =
44874487
}
44884488
$substitarray['__SMTP_SPF_STRING__'] = '_spf'.$sldAndSubdomain.'.'.$domainname;
44894489

4490-
4490+
44914491
$dirfortmpfiles = DOL_DATA_ROOT.'/sellyoursaas/temp';
44924492
dol_mkdir($dirfortmpfiles, '', '0775');
44934493
$tmppackage->srcconffile1 = $dirfortmpfiles.'/conf.php.'.$sldAndSubdomain.'.'.$domainname.'.tmp';
44944494
$tmppackage->srccronfile = $dirfortmpfiles.'/cron.'.$sldAndSubdomain.'.'.$domainname.'.tmp';
44954495
$tmppackage->srccliafter = $dirfortmpfiles.'/cliafter.'.$sldAndSubdomain.'.'.$domainname.'.tmp';
44964496
$tmppackage->srccliafterpaid = $dirfortmpfiles.'/cliafterpaid.'.$sldAndSubdomain.'.'.$domainname.'.tmp';
4497-
4497+
44984498
$conffile = make_substitutions($tmppackage->conffile1, $substitarray);
44994499
$cronfile = make_substitutions($tmppackage->crontoadd, $substitarray);
45004500
$cliafter = make_substitutions($tmppackage->cliafter, $substitarray);
@@ -4708,7 +4708,7 @@ public function sellyoursaasRemoteAction($remoteaction, $object, $appusername =
47084708
$generateddbprefix = ($contract->array_options['options_prefix_db'] ? $contract->array_options['options_prefix_db'] : 'llx_');
47094709
$generatedunixhostname= $contract->array_options['options_hostname_os'];
47104710
$generateddbhostname = $contract->array_options['options_hostname_db'];
4711-
$generateduniquekey = getRandomPassword(true);
4711+
$generateduniquekey = empty($contract->array_options['options_instance_unique_id']) ? getRandomPassword(true) : $contract->array_options['options_instance_unique_id'];
47124712

47134713
$sshaccesstype = (empty($contract->array_options['options_sshaccesstype']) ? 0 : $contract->array_options['options_sshaccesstype']);
47144714
$customurl = $contract->array_options['options_custom_url'];

core/modules/modSellYourSaas.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,11 @@ public function init($options = '')
971971
$param=array('options'=>array('dolcrypt'=>null));
972972
$resultx=$extrafields->addExtraField('password_os', "Password OS", 'password', 127, '128', 'contrat', 0, 0, '', $param, 1, '', -1, 0, '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
973973
$param=array('options'=>array('0'=>'SystemDefault','1'=>'CommonUserJail','2'=>'PrivateUserJail'));
974+
$resultx=$extrafields->addExtraField('instance_unique_id', "Instance unique ID", 'varchar', 128, '32', 'contrat', 1, 0, '', '', 1, '', -1, 0, '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
974975

975-
$resultx=$extrafields->addExtraField('sshaccesstype', "SshAccessType", 'select', 128, '', 'contrat', 0, 0, '', $param, 1, '', 'getDolGlobalInt("SELLYOURSAAS_SSH_JAILKIT_ENABLED")', 'HelpOnSshAccessType', '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
976+
$resultx=$extrafields->addExtraField('sshaccesstype', "SshAccessType", 'select', 400, '', 'contrat', 0, 0, '', $param, 1, '', 'getDolGlobalInt("SELLYOURSAAS_SSH_JAILKIT_ENABLED")', 'HelpOnSshAccessType', '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
976977
$param=array('options'=>array('0'=>'DefaultFromAppService','1'=>'Yes'));
977-
$resultx=$extrafields->addExtraField('directaccess', "AccessToResources", 'select', 129, '', 'contrat', 0, 0, '', $param, 1, '', -1, 0, '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
978+
$resultx=$extrafields->addExtraField('directaccess', "AccessToResources", 'select', 401, '', 'contrat', 0, 0, '', $param, 1, '', -1, 0, '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
978979

979980
$resultx=$extrafields->addExtraField('hostname_db', "Hostname DB", 'varchar', 130, '128', 'contrat', 0, 0, '', '', 1, '', -1, 0, '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');
980981
$resultx=$extrafields->addExtraField('database_db', "Database DB", 'varchar', 131, '32', 'contrat', 1, 0, '', '', 1, '', -1, 0, '', '', 'sellyoursaas@sellyoursaas', 'isModEnabled("sellyoursaas")');

myaccount/register_instance.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,14 @@
226226
// If ran from command line
227227
if (substr($sapi_type, 0, 3) == 'cli') {
228228
$productref = $argv[1];
229-
$instancefullname = $argv[2];
229+
$instancefullname = $argv[2]; // New name of instance
230230
$instancefullnamearray = explode('.', $instancefullname);
231231
$sldAndSubdomain = $instancefullnamearray[0];
232232
unset($instancefullnamearray[0]);
233233
$tldid = '.'.join('.', $instancefullnamearray);
234234
$password = $argv[3];
235-
$reusesocid = $argv[4];
236-
$customurl = $argv[5];
235+
$reusesocid = $argv[4]; // Thirdparty id to reuse
236+
$customurl = $argv[5]; // Old instance name or Custom url
237237
if (empty($productref) || empty($sldAndSubdomain) || empty($tldid) || empty($password) || empty($reusesocid)) {
238238
print "***** ".$script_file." *****\n";
239239
print "Create an instance from command line. Run this script from the master server. Note: No email are sent to customer.\n";
@@ -329,7 +329,7 @@
329329
$newurl.='&mode=instances';
330330
$newurl.='&reusecontractid='.((int) $reusecontractid);
331331
} elseif ($reusesocid) { // Can be >= 0, but also -1
332-
// When we use the "Add another instance" from the "myaccount" dashboard
332+
// When we use the "Add another instance" from the "myaccount" dashboard or using the master_move_instance.php
333333

334334
// Check we are logged and that reusesocid is not forged
335335
if (substr($sapi_type, 0, 3) != 'cli') {
@@ -1358,6 +1358,33 @@
13581358
//dol_syslog("options_deployment_ipquality = ".$contract->array_options['options_deployment_ipquality'], LOG_DEBUG);
13591359
//dol_syslog("options_deployment_emailquality = ".$contract->array_options['options_deployment_emailquality'], LOG_DEBUG);
13601360

1361+
1362+
// If we have a customurl, it may be an alias or an old instance name we plan to move.
1363+
if ($customurl) {
1364+
// We scan database to find old instance
1365+
$sqltogetoldinstanceid = "SELECT ce.instance_unique_id FROM ".MAIN_DB_PREFIX."contrat_extrafields as ce, ".MAIN_DB_PREFIX."contrat as c";
1366+
$sqltogetoldinstanceid .= " WHERE c.rowid = ce.fk_object AND c.ref_customer = '".$db->escape($customurl)."'";
1367+
$sqltogetoldinstanceid .= " LIMIT 1"; // We should always have only 1.
1368+
1369+
$resqltogetoldinstanceid = $db->query($sqltogetoldinstanceid);
1370+
if ($resqltogetoldinstanceid) {
1371+
$objtogetoldinstanceid = $db->fetch_object($resqltogetoldinstanceid);
1372+
if ($objtogetoldinstanceid) {
1373+
$old_instance_unique_id = $objtogetoldinstanceid->instance_unique_id;
1374+
1375+
$contract->array_options['options_instance_unique_id'] = $old_instance_unique_id;
1376+
}
1377+
} else {
1378+
print "SQL error when trying to find the options_instance_unique_id of old instance";
1379+
exit(-89);
1380+
}
1381+
1382+
dol_syslog("We will reuse the options_instance_unique_id = ".$contract->array_options['options_instance_unique_id']." from ".$customurl, LOG_DEBUG);
1383+
} else {
1384+
dol_syslog("No custom url provided or url is not an oldinstance so we won't reuse an existing value for options_instance_unique_id", LOG_DEBUG);
1385+
}
1386+
1387+
13611388
if ($abusetest) {
13621389
$db->rollback();
13631390

scripts/master_move_instance.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@
523523
$newpass = getRandomPassword(true, array('I'), 16);
524524
}
525525

526+
// Create virgin envelop for the new instance. The register_instance will use the $oldinstance name
526527
$command='php '.DOL_DOCUMENT_ROOT."/custom/sellyoursaas/myaccount/register_instance.php ".escapeshellarg($productref)." ".escapeshellarg($newinstance)." ".escapeshellarg($newpass)." ".escapeshellarg($oldobject->thirdparty->id);
527528
$commandnopass='php '.DOL_DOCUMENT_ROOT."/custom/sellyoursaas/myaccount/register_instance.php ".escapeshellarg($productref)." ".escapeshellarg($newinstance)." --a-new-password-- ".escapeshellarg($oldobject->thirdparty->id);
528529
$command.=" ".escapeshellarg($oldinstance);

0 commit comments

Comments
 (0)