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 | 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 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 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x | import dedent from 'dedent';
import { z } from 'zod';
import { getOperator } from '../../clever-client/operators.js';
import { config } from '../../config/config.js';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as Addon from '../../models/addon.js';
import { completePlan, completeRegion, parseAddonOptions } from '../../models/addon.js';
import * as AppConfig from '../../models/app_configuration.js';
import { listAvailableAliases } from '../../models/application.js';
import { getOwnerIdFromOrgIdOrName } from '../../models/ids-resolver.js';
import { sendToApi } from '../../models/send-to-api.js';
import { addonOptions } from '../../parsers.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
import { addonNameArg, addonProviderArg } from './addon.args.js';
const ADDON_PROVIDERS = {
keycloak: {
name: 'Keycloak',
operatorProvider: 'keycloak',
postCreateInstructions: `Learn more about Keycloak on Clever Cloud: ${config.DOC_URL}/addons/keycloak/`,
},
kv: {
name: 'Materia KV',
status: 'beta',
postCreateInstructions: (addonId) => dedent`
${styleText('yellow', "You can easily use Materia KV with 'redis-cli', with such commands:")}
${styleText('blue', `source <(clever addon env ${addonId} -F shell)`)}
${styleText('blue', 'redis-cli -h $KV_HOST -p $KV_PORT --tls')}
Learn more about Materia KV on Clever Cloud: ${config.DOC_URL}/addons/materia-kv/
`,
},
'addon-matomo': {
name: 'Matomo',
operatorProvider: 'matomo',
postCreateInstructions: `Learn more about Matomo on Clever Cloud: ${config.DOC_URL}/addons/matomo/`,
},
metabase: {
name: 'Metabase',
operatorProvider: 'metabase',
postCreateInstructions: `Learn more about Metabase on Clever Cloud: ${config.DOC_URL}/addons/metabase/`,
},
otoroshi: {
name: 'Otoroshi with LLM',
operatorProvider: 'otoroshi',
postCreateInstructions: `Learn more about Otoroshi with LLM on Clever Cloud: ${config.DOC_URL}/addons/otoroshi/`,
},
'addon-pulsar': {
name: 'Pulsar',
postCreateInstructions: `Learn more about Pulsar on Clever Cloud: ${config.DOC_URL}/addons/pulsar/`,
},
};
export const addonCreateCommand = defineCommand({
description: 'Create an add-on',
since: '0.2.3',
options: {
link: defineOption({
name: 'link',
schema: z.string().optional(),
description: 'Link the created add-on to the app with the specified alias',
aliases: ['l'],
placeholder: 'alias',
complete: listAvailableAliases,
}),
yes: defineOption({
name: 'yes',
schema: z.boolean().default(false),
description: 'Skip confirmation even if the add-on is not free',
aliases: ['y'],
}),
plan: defineOption({
name: 'plan',
schema: z.string().default(''),
description: 'Add-on plan, depends on the provider',
aliases: ['p'],
placeholder: 'plan',
complete: completePlan,
}),
region: defineOption({
name: 'region',
schema: z.string().default('par'),
description: 'Region to provision the add-on in, depends on the provider',
aliases: ['r'],
placeholder: 'region',
complete: completeRegion,
}),
addonVersion: defineOption({
name: 'addon-version',
schema: z.string().optional(),
description: 'The version to use for the add-on',
placeholder: 'addon-version',
}),
option: defineOption({
name: 'option',
schema: z
.union([z.string(), z.array(z.string())])
.transform(addonOptions)
.optional(),
description:
'Option to enable for the add-on. Multiple --option argument can be passed to enable multiple options',
placeholder: 'option',
}),
org: orgaIdOrNameOption,
format: humanJsonOutputFormatOption,
},
args: [addonProviderArg, addonNameArg],
async handler(options, providerName, name) {
const {
link: linkedAppAlias,
plan: planName,
region,
yes: skipConfirmation,
org: orgaIdOrName,
format,
addonVersion,
option: addonOptions,
} = options;
const addonToCreate = {
name,
providerName,
planName,
region,
skipConfirmation,
version: addonVersion,
addonOptions: parseAddonOptions(addonOptions),
};
const ownerId = await getOwnerIdFromOrgIdOrName(orgaIdOrName);
if (linkedAppAlias != null) {
const linkedAppData = await AppConfig.getAppDetails({ alias: linkedAppAlias });
if (orgaIdOrName != null && linkedAppData.ownerId !== ownerId && format === 'human') {
Logger.warn(
'The specified application does not belong to the specified organisation. Ignoring the `--org` option',
);
}
addonToCreate.ownerId = linkedAppData.ownerId;
} else {
addonToCreate.ownerId = ownerId;
}
const newAddon = await Addon.create(addonToCreate);
if (linkedAppAlias != null) {
const linkedAppData = await AppConfig.getAppDetails({ alias: linkedAppAlias });
await Addon.link(linkedAppData.ownerId, linkedAppData.appId, { addon_id: newAddon.id });
}
const provider = ADDON_PROVIDERS[providerName];
let statusMessage = '';
if (provider?.status) {
statusMessage = `The ${provider.name} provider is in ${provider.status} testing phase`;
if (provider.status === 'alpha') {
statusMessage += ". Don't store sensitive or production grade data.";
}
}
// JSON format
if (format === 'json') {
const jsonAddon = {
id: newAddon.id,
realId: newAddon.realId,
name: newAddon.name,
env: newAddon.env,
};
if (provider?.status) {
jsonAddon.availability = provider.status;
jsonAddon.message = statusMessage;
}
Logger.printJson(jsonAddon);
return;
}
// Human format
if (linkedAppAlias != null) {
Logger.println(`Add-on created and linked to application ${linkedAppAlias} successfully!`);
} else {
Logger.println('Add-on created successfully!');
}
Logger.println(`ID: ${newAddon.id}`);
Logger.println(`Real ID: ${newAddon.realId}`);
Logger.println(`Name: ${newAddon.name}`);
const operatorProvider = ADDON_PROVIDERS[providerName]?.operatorProvider;
const operator =
operatorProvider != null
? await getOperator({ provider: operatorProvider, realId: newAddon.realId }).then(sendToApi)
: null;
if (operator != null) {
if (operator.accessUrl != null) {
Logger.println();
Logger.println(`Your ${ADDON_PROVIDERS[providerName].name} is starting:`);
Logger.println(` - Access it: ${operator.accessUrl}`);
Logger.println(` - Manage it: ${config.GOTO_URL}/${newAddon.id}`);
}
if (operator.initialCredentials) {
if (providerName === 'keycloak') {
Logger.println();
Logger.println(
"An initial account has been created, you'll be invited to change the password at first login:",
);
Logger.println(` - Admin user name: ${operator.initialCredentials.user}`);
Logger.println(` - Temporary password: ${operator.initialCredentials.password}`);
}
if (providerName === 'otoroshi') {
Logger.println();
Logger.println(
'An initial account has been created, change the password at first login (Security -> Administrators -> Edit user):',
);
Logger.println(` - Admin user name: ${operator.initialCredentials.user}`);
Logger.println(` - Password: ${operator.initialCredentials.password}`);
}
}
}
if (provider?.postCreateInstructions) {
Logger.println();
if (statusMessage !== '') {
Logger.println(styleText('yellow', `/!\\ ${statusMessage}`));
}
if (typeof provider.postCreateInstructions === 'function') {
Logger.println(provider.postCreateInstructions(newAddon.id));
} else {
Logger.println(provider.postCreateInstructions);
}
}
},
});
|