All files / src/models application.js

37.26% Statements 136/365
82.75% Branches 24/29
20% Functions 5/25
37.26% Lines 136/365

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                   193x 193x 193x 193x     193x 193x     193x 193x     193x               193x                         193x 193x                                                           193x 193x                         193x 193x                                       193x                               193x                   193x         193x 4x 4x 4x 4x 4x 193x 193x 6x 6x 6x 193x 193x       193x 4x 4x 4x 4x 4x 4x 4x 193x 193x 193x 193x 193x 193x 193x 49x 3x 3x 46x 46x 46x 46x 25x 16x 16x 21x 21x 21x 21x 12x 9x 6x 6x 6x 6x 6x 3x 3x 3x 9x 9x 9x 9x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x             49x 193x 193x 6x 6x 6x 4x 6x     6x 193x 193x       193x 193x         193x 193x                                                                 193x 193x                                           193x 193x                             193x 193x       193x 193x        
import {
  addDependency,
  create as createApplication,
  getAll as getAllApplications,
  getAllDependencies,
  get as getApplication,
  redeploy as redeployApplication,
  remove as removeApplication,
  removeDependency,
  update as updateApplication,
} from '@clevercloud/client/esm/api/v2/application.js';
import { getAvailableInstances } from '@clevercloud/client/esm/api/v2/product.js';
import { getSummary } from '@clevercloud/client/esm/api/v2/user.js';
import _ from 'lodash';
import { confirmAnswer } from '../lib/prompts.js';
import { styleText } from '../lib/style-text.js';
import { Logger } from '../logger.js';
import * as AppConfiguration from './app_configuration.js';
import { resolveOwnerId } from './ids-resolver.js';
import * as Organisation from './organisation.js';
import { sendToApi } from './send-to-api.js';
import * as User from './user.js';
 
export function listAvailableTypes() {
  return [
    'docker',
    'elixir',
    'frankenphp',
    'go',
    'gradle',
    'haskell',
    'jar',
    'linux',
    'maven',
    'meteor',
    'node',
    'php',
    'play1',
    'play2',
    'python',
    'ruby',
    'rust',
    'sbt',
    'static',
    'static-apache',
    'v',
    'war',
  ];
}
 
export const AVAILABLE_ZONES = ['par', 'parhds', 'grahds', 'rbx', 'rbxhds', 'scw', 'ldn', 'mtl', 'sgp', 'syd', 'wsw'];
 
export function listAvailableZones() {
  return AVAILABLE_ZONES;
}
 
export function listAvailableAliases() {
  return AppConfiguration.loadApplicationConf().then(({ apps }) => _.map(apps, 'alias'));
}
 
export function listAvailableFlavors() {
  return ['pico', 'nano', 'XS', 'S', 'M', 'L', 'XL', '2XL', '3XL'];
}
 
async function getId(ownerId, dependency) {
  if (dependency.app_id) {
    return dependency.app_id;
  }
  const app = await getByName(ownerId, dependency.app_name);
  return app.id;
}
 
async function getInstanceType(type) {
  // TODO: We should be able to use it without {}
  const types = await getAvailableInstances({}).then(sendToApi);

  const enabledTypes = types.filter((t) => t.enabled);
  const matchingVariants = enabledTypes.filter((t) => t.variant != null && t.variant.slug === type);
  const instanceVariant = _.sortBy(matchingVariants, 'version').reverse()[0];
  if (instanceVariant == null) {
    throw new Error(type + ' type does not exist.');
  }
  return instanceVariant;
}
 
export async function create(name, typeName, region, orgaIdOrName, github, isTask, envVars) {
  Logger.debug('Create the application…');

  const ownerId = orgaIdOrName != null ? await Organisation.getId(orgaIdOrName) : await User.getCurrentId();

  const instanceType = await getInstanceType(typeName);

  const newApp = {
    deploy: 'git',
    description: name,
    instanceType: instanceType.type,
    instanceVersion: instanceType.version,
    instanceVariant: instanceType.variant.id,
    maxFlavor: instanceType.defaultFlavor.name,
    maxInstances: 1,
    minFlavor: instanceType.defaultFlavor.name,
    minInstances: 1,
    name: name,
    zone: region,
    instanceLifetime: isTask ? 'TASK' : 'REGULAR',
    env: envVars,
  };

  if (github != null) {
    newApp.oauthService = 'github';
    newApp.oauthApp = github;
  }

  return createApplication({ id: ownerId }, newApp).then(sendToApi);
}
 
export async function deleteApp(app, skipConfirmation) {
  Logger.debug('Deleting app: ' + app.name + ' (' + app.id + ')');

  if (!skipConfirmation) {
    await confirmAnswer(
      `Deleting an application can't be undone, please type ${styleText('green', app.name)} to confirm:`,
      'No confirmation, aborting application deletion',
      app.name,
    );
  }

  return removeApplication({ id: app.ownerId, appId: app.id }).then(sendToApi);
}
 
export async function getAllApps(ownerId) {
  const summary = await getSummary().then(sendToApi);

  const orgaWithApps = await Promise.all(
    summary.organisations
      // If owner ID is present, only keep the matching org
      .filter((org) => ownerId == null || org.id === ownerId)
      .sort((a, b) => a.name.localeCompare(b.name))
      .map(async (org) => {
        const applications = await getApplicationsForOwner(org.id);
        return {
          id: org.id,
          name: org.name,
          applications: applications.sort((a, b) => a.name.localeCompare(b.name)),
        };
      }),
  );

  return orgaWithApps;
}
 
async function getApplicationsForOwner(ownerId) {
  const rawApplications = await getAllApplications({ id: ownerId }).then(sendToApi);
  return rawApplications.map((app) => {
    return {
      app_id: app.id,
      org_id: ownerId,
      name: app.name,
      zone: app.zone,
      type: app.instance.variant.slug,
      createdAt: new Date(app.creationDate).toISOString(),
      deploy_url: app.deployment.httpUrl,
      git_ssh_url: app.deployment.url,
    };
  });
}
 
function getApplicationByName(apps, name) {
  const filteredApps = apps.filter((app) => app.name === name);
  if (filteredApps.length === 1) {
    return filteredApps[0];
  } else if (filteredApps.length === 0) {
    throw new Error('Application not found');
  }
  throw new Error('Ambiguous application name');
}
 
async function getByName(ownerId, name) {
  const apps = await getAllApplications({ id: ownerId }).then(sendToApi);
  return getApplicationByName(apps, name);
}
 
function addInstanceLifetime(app) {
  // Patch to help config commands
  app.instanceLifetime = app.instance.lifetime;
  return app;
}
 
export function get(ownerId, appId) {
  Logger.debug(`Get information for the app: ${appId}`);
  return getApplication({ id: ownerId, appId }).then(sendToApi).then(addInstanceLifetime);
}
 
export function updateOptions(ownerId, appId, options) {
  Logger.debug(`Update app: ${appId}`);
  return updateApplication({ id: ownerId, appId }, options).then(sendToApi).then(addInstanceLifetime);
}
 
function getFromSelf(appId) {
  Logger.debug(`Get information for the app: ${appId}`);
  // /self differs from /organisations only for this one:
  // it fallbacks to the organisations of which the user
  // is a member, if it doesn't belong to Personal Space.
  return getApplication({ appId }).then(sendToApi);
}
 
/**
 * @param {{app_id: string}|{app_name: string}} appIdOrName
 * @param {string} alias
 * @return {Promise<{appId: string, ownerId: string}>}
 */
export async function resolveId(appIdOrName, alias) {
  if (appIdOrName != null && alias != null) {
    throw new Error('Only one of the `--app` or `--alias` options can be set at a time');
  }
 
  // -- resolve by linked app
 
  if (appIdOrName == null) {
    const appDetails = await AppConfiguration.getAppDetails({ alias });
    return { appId: appDetails.appId, ownerId: appDetails.ownerId };
  }
 
  // -- resolve by app id
 
  if (appIdOrName.app_id != null) {
    const ownerId = await resolveOwnerId(appIdOrName.app_id);
    if (ownerId != null) {
      return {
        appId: appIdOrName.app_id,
        ownerId,
      };
    }
 
    throw new Error('Application not found');
  }
 
  // -- resolve by app name
 
  const summary = await getSummary({}).then(sendToApi);
 
  const candidates = [summary.user, ...summary.organisations]
    .flatMap((owner) => owner.applications.map((app) => ({ app, owner })))
    .filter((candidate) => candidate.app.name === appIdOrName.app_name);
 
  if (candidates.length === 0) {
    throw new Error('Application not found');
  }
  if (candidates.length === 1) {
    return {
      appId: candidates[0].app.id,
      ownerId: candidates[0].owner.id,
    };
  }

  Logger.printErrorLine(`The name '${appIdOrName.app_name}' refers to multiple applications:`);
  candidates.forEach((candidate) => {
    Logger.printErrorLine(`- ${candidate.owner.name}: ${candidate.app.id} (${candidate.app.variantSlug})`);
  });
  throw new Error('Ambiguous application name, use the `--app` option with one of the IDs above');
}
 
export async function linkRepo(app, orgaIdOrName, alias, ignoreParentConfig) {
  Logger.debug(`Linking current repository to the app: ${app.app_id || app.app_name}`);
 
  const ownerId = orgaIdOrName != null ? await Organisation.getId(orgaIdOrName) : await User.getCurrentId();
 
  const appData = app.app_id != null ? await getFromSelf(app.app_id) : await getByName(ownerId, app.app_name);

  return AppConfiguration.addLinkedApplication(appData, alias, ignoreParentConfig);
}
 
export function unlinkRepo(alias) {
  Logger.debug(`Unlinking current repository from the app: ${alias}`);
  return AppConfiguration.removeLinkedApplication({ alias });
}
 
export function redeploy(ownerId, appId, commit, withoutCache) {
  Logger.debug(`Redeploying the app: ${appId}`);
  const useCache = withoutCache ? 'no' : null;
  return redeployApplication({ id: ownerId, appId, commit, useCache }).then(sendToApi);
}
 
export function mergeScalabilityParameters(scalabilityParameters, instance) {
  const flavors = listAvailableFlavors();

  if (scalabilityParameters.minFlavor) {
    instance.minFlavor = scalabilityParameters.minFlavor;
    if (flavors.indexOf(instance.minFlavor) > flavors.indexOf(instance.maxFlavor)) {
      instance.maxFlavor = instance.minFlavor;
    }
  }
  if (scalabilityParameters.maxFlavor) {
    instance.maxFlavor = scalabilityParameters.maxFlavor;
    if (
      flavors.indexOf(instance.minFlavor) > flavors.indexOf(instance.maxFlavor) &&
      scalabilityParameters.minFlavor == null
    ) {
      instance.minFlavor = instance.maxFlavor;
    }
  }

  if (scalabilityParameters.minInstances) {
    instance.minInstances = scalabilityParameters.minInstances;
    if (instance.minInstances > instance.maxInstances) {
      instance.maxInstances = instance.minInstances;
    }
  }
  if (scalabilityParameters.maxInstances) {
    instance.maxInstances = scalabilityParameters.maxInstances;
    if (instance.minInstances > instance.maxInstances && scalabilityParameters.minInstances == null) {
      instance.minInstances = instance.maxInstances;
    }
  }
  return instance;
}
 
export async function setScalability(appId, ownerId, scalabilityParameters, buildFlavor) {
  Logger.info('Scaling the app: ' + appId);

  const app = await getApplication({ id: ownerId, appId }).then(sendToApi);
  const instance = _.cloneDeep(app.instance);

  instance.minFlavor = instance.minFlavor.name;
  instance.maxFlavor = instance.maxFlavor.name;

  const newConfig = mergeScalabilityParameters(scalabilityParameters, instance);

  if (buildFlavor != null) {
    newConfig.separateBuild = buildFlavor !== 'disabled';
    if (buildFlavor !== 'disabled') {
      newConfig.buildFlavor = buildFlavor;
    } else {
      Logger.info('No build size given, disabling dedicated build instance');
    }
  }

  return updateApplication({ id: ownerId, appId }, newConfig).then(sendToApi);
}
 
export async function listDependencies(ownerId, appId, showAll) {
  const applicationDeps = await getAllDependencies({ id: ownerId, appId }).then(sendToApi);

  if (!showAll) {
    return applicationDeps.map((app) => ({ ...app, isLinked: true }));
  }

  const allApps = await getAllApplications({ id: ownerId }).then(sendToApi);

  const applicationDepsIds = applicationDeps.map((app) => app.id);
  return allApps.map((app) => {
    const isLinked = applicationDepsIds.includes(app.id);
    return { ...app, isLinked };
  });
}
 
export async function link(ownerId, appId, dependency) {
  const dependencyId = await getId(ownerId, dependency);
  return addDependency({ id: ownerId, appId, dependencyId }).then(sendToApi);
}
 
export async function unlink(ownerId, appId, dependency) {
  const dependencyId = await getId(ownerId, dependency);
  return removeDependency({ id: ownerId, appId, dependencyId }).then(sendToApi);
}