Skip to content

Commit

Permalink
Merge pull request #243 from mittwald/feature/import-files-db
Browse files Browse the repository at this point in the history
Add support for uploading app files + databases
  • Loading branch information
martin-helmich authored Apr 9, 2024
2 parents bc2e1cd + c2b21c0 commit bafb095
Show file tree
Hide file tree
Showing 8 changed files with 534 additions and 101 deletions.
108 changes: 105 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ USAGE
* [`mw app list`](#mw-app-list)
* [`mw app ssh [INSTALLATION-ID]`](#mw-app-ssh-installation-id)
* [`mw app uninstall [INSTALLATION-ID]`](#mw-app-uninstall-installation-id)
* [`mw app upload [INSTALLATION-ID]`](#mw-app-upload-installation-id)
* [`mw app versions [APP]`](#mw-app-versions-app)
* [`mw autocomplete [SHELL]`](#mw-autocomplete-shell)
* [`mw backup create`](#mw-backup-create)
Expand Down Expand Up @@ -170,6 +171,7 @@ USAGE
* [`mw database mysql delete DATABASE-ID`](#mw-database-mysql-delete-database-id)
* [`mw database mysql dump DATABASE-ID`](#mw-database-mysql-dump-database-id)
* [`mw database mysql get DATABASE-ID`](#mw-database-mysql-get-database-id)
* [`mw database mysql import DATABASE-ID`](#mw-database-mysql-import-database-id)
* [`mw database mysql list`](#mw-database-mysql-list)
* [`mw database mysql phpmyadmin DATABASE-ID`](#mw-database-mysql-phpmyadmin-database-id)
* [`mw database mysql port-forward DATABASE-ID`](#mw-database-mysql-port-forward-database-id)
Expand Down Expand Up @@ -1781,6 +1783,47 @@ FLAG DESCRIPTIONS
scripts), you can use this flag to easily get the IDs of created resources for further processing.
```

## `mw app upload [INSTALLATION-ID]`

Upload the filesystem of an app to a project

```
USAGE
$ mw app upload [INSTALLATION-ID] --source <value> [-q] [--ssh-user <value>] [--dry-run] [--delete]
ARGUMENTS
INSTALLATION-ID ID or short ID of an app installation; this argument is optional if a default app installation is set
in the context
FLAGS
-q, --quiet suppress process output and only display a machine-readable summary.
--delete delete remote files that are not present locally
--dry-run do not actually upload the app installation
--source=<value> (required) source directory from which to upload the app installation
--ssh-user=<value> override the SSH user to connect with; if omitted, your own user will be used
DESCRIPTION
Upload the filesystem of an app to a project
Upload the filesystem of an app from your local machine to a project.
CAUTION: This is a potentially destructive operation. It will overwrite files on the server with the files from your
local machine. This is NOT a turnkey deployment solution. It is intended for development purposes only.
FLAG DESCRIPTIONS
-q, --quiet suppress process output and only display a machine-readable summary.
This flag controls if you want to see the process output or only a summary. When using mw non-interactively (e.g. in
scripts), you can use this flag to easily get the IDs of created resources for further processing.
--ssh-user=<value> override the SSH user to connect with; if omitted, your own user will be used
This flag can be used to override the SSH user that is used for a connection; be default, your own personal user
will be used for this.
You can also set this value by setting the MITTWALD_SSH_USER environment variable.
```

## `mw app versions [APP]`

List supported Apps and Versions
Expand Down Expand Up @@ -2540,7 +2583,7 @@ Create a dump of a MySQL database

```
USAGE
$ mw database mysql dump DATABASE-ID -o <value> [-q] [-p <value>] [--ssh-user <value>] [--temporary-user] [--gzip]
$ mw database mysql dump DATABASE-ID -o <value> [-q] [-p <value>] [--temporary-user] [--ssh-user <value>] [--gzip]
ARGUMENTS
DATABASE-ID The ID or name of the database
Expand Down Expand Up @@ -2587,8 +2630,8 @@ FLAG DESCRIPTIONS
--[no-]temporary-user create a temporary user for the dump
Create a temporary user for the dump. This user will be deleted after the dump has been created. This is useful if
you want to dump a database that is not accessible from the outside.
Create a temporary user for this operation. This user will be deleted after the operation has completed. This is
useful if you want to work with a database that is not accessible from the outside.
If this flag is disabled, you will need to specify the password of the default user; either via the --mysql-password
flag or via the MYSQL_PWD environment variable.
Expand All @@ -2613,6 +2656,65 @@ DESCRIPTION
Get a MySQLDatabase.
```

## `mw database mysql import DATABASE-ID`

Imports a dump of a MySQL database

```
USAGE
$ mw database mysql import DATABASE-ID -i <value> [-q] [-p <value>] [--temporary-user] [--ssh-user <value>] [--gzip]
ARGUMENTS
DATABASE-ID The ID or name of the database
FLAGS
-i, --input=<value> (required) the input file from which to read the dump ("-" for stdin)
-p, --mysql-password=<value> the password to use for the MySQL user (env: MYSQL_PWD)
-q, --quiet suppress process output and only display a machine-readable summary.
--gzip uncompress the dump with gzip
--ssh-user=<value> override the SSH user to connect with; if omitted, your own user will be used
--[no-]temporary-user create a temporary user for the dump
FLAG DESCRIPTIONS
-i, --input=<value> the input file from which to read the dump ("-" for stdin)
The input file from which to read the dump to. You can specify "-" or "/dev/stdin" to read the dump directly from
STDIN.
-p, --mysql-password=<value> the password to use for the MySQL user (env: MYSQL_PWD)
The password to use for the MySQL user. If not provided, the environment variable MYSQL_PWD will be used. If that is
not set either, the command will interactively ask for the password.
NOTE: This is a security risk, as the password will be visible in the process list of your system, and will be
visible in your Shell history. It is recommended to use the environment variable instead.
-q, --quiet suppress process output and only display a machine-readable summary.
This flag controls if you want to see the process output or only a summary. When using mw non-interactively (e.g. in
scripts), you can use this flag to easily get the IDs of created resources for further processing.
--gzip uncompress the dump with gzip
Uncompress the dump with gzip while importing. This is useful for large databases, as it can significantly reduce
the size of the dump.
--ssh-user=<value> override the SSH user to connect with; if omitted, your own user will be used
This flag can be used to override the SSH user that is used for a connection; be default, your own personal user
will be used for this.
You can also set this value by setting the MITTWALD_SSH_USER environment variable.
--[no-]temporary-user create a temporary user for the dump
Create a temporary user for this operation. This user will be deleted after the operation has completed. This is
useful if you want to work with a database that is not accessible from the outside.
If this flag is disabled, you will need to specify the password of the default user; either via the --mysql-password
flag or via the MYSQL_PWD environment variable.
```

## `mw database mysql list`

List MySQLDatabases belonging to a Project.
Expand Down
110 changes: 110 additions & 0 deletions src/commands/app/upload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { ExecRenderBaseCommand } from "../../rendering/react/ExecRenderBaseCommand.js";
import { appInstallationArgs } from "../../lib/app/flags.js";
import {
makeProcessRenderer,
processFlags,
} from "../../rendering/process/process_flags.js";
import { Flags } from "@oclif/core";
import { Success } from "../../rendering/react/components/Success.js";
import { ReactNode } from "react";
import { hasBinary } from "../../lib/hasbin.js";
import { getSSHConnectionForAppInstallation } from "../../lib/ssh/appinstall.js";
import { spawnInProcess } from "../../rendering/process/process_exec.js";
import { sshConnectionFlags } from "../../lib/ssh/flags.js";

export class Upload extends ExecRenderBaseCommand<typeof Upload, void> {
static summary = "Upload the filesystem of an app to a project";
static description =
"Upload the filesystem of an app from your local machine to a project.\n\n" +
"" +
"CAUTION: This is a potentially destructive operation. It will overwrite files on the server with the files from your local machine. " +
"This is NOT a turnkey deployment solution. It is intended for development purposes only.";
static args = {
...appInstallationArgs,
};
static flags = {
...processFlags,
...sshConnectionFlags,
"dry-run": Flags.boolean({
description: "do not actually upload the app installation",
default: false,
}),
delete: Flags.boolean({
description: "delete remote files that are not present locally",
default: false,
}),
source: Flags.directory({
description: "source directory from which to upload the app installation",
required: true,
exists: true,
}),
};

protected async exec(): Promise<void> {
const appInstallationId = await this.withAppInstallationId(Upload);
const {
"dry-run": dryRun,
source,
delete: deleteRemote,
"ssh-user": sshUser,
} = this.flags;

const p = makeProcessRenderer(this.flags, "Uploading app installation");

const { host, user, directory } = await p.runStep(
"getting connection data",
async () => {
return getSSHConnectionForAppInstallation(
this.apiClient,
appInstallationId,
sshUser,
);
},
);

await p.runStep("check if rsync is installed", async () => {
if (!(await hasBinary("rsync"))) {
throw new Error("this command requires rsync to be installed");
}
});

const rsyncOpts = [
"--archive",
"--recursive",
"--verbose",
"--progress",
"--exclude=typo3temp",
];
if (dryRun) {
rsyncOpts.push("--dry-run");
}
if (deleteRemote) {
rsyncOpts.push("--delete");
}

await spawnInProcess(
p,
"uploading app installation" + (dryRun ? " (dry-run)" : ""),
"rsync",
[...rsyncOpts, source, `${user}@${host}:${directory}/`],
);

await p.complete(<UploadSuccess dryRun={dryRun} />);
}

protected render(): ReactNode {
return undefined;
}
}

function UploadSuccess({ dryRun }: { dryRun: boolean }) {
if (dryRun) {
return (
<Success>
App would (probably) have successfully been uploaded. 🙂
</Success>
);
}

return <Success>App successfully uploaded; have fun! 🚀</Success>;
}
Loading

0 comments on commit bafb095

Please sign in to comment.