import Tabs from '@/components/docs/Tabs.astro';
import Steps from '@/components/docs/Steps.astro';
import FileTree from '@/components/docs/FileTree.astro';
import Image from '@/components/docs/Image.astro';
import Alert from '@/components/docs/Alert.astro';
import Details from '@/components/docs/Details.astro';
import Cards from '@/components/docs/Cards.astro';
import Card from '@/components/docs/Card.astro';

<Alert type="note">
  Before you begin, make sure the server and clients meet the [system
  requirements](/en/platform/v3/requirements).
</Alert>

## Downloading the installation files

You can download the IIoT Platform installation images in two ways.

<Tabs labels={['From external media', 'Over the Internet']}>
  <Fragment slot="panel-0">
    When the server has no Internet access — for example, under an enterprise security policy — you
    can install the platform offline. The installation images are transferred to the server from
    external media.
  </Fragment>
  <Fragment slot="panel-1">
    When the server has Internet access, the installation images are pulled onto the server from a
    remote repository.
  </Fragment>
</Tabs>

## Installation steps

<Steps>

### Copy the required data package

Copy the installation package to the server (administrator rights are required):

- Copy the downloaded `unicloud.tgz` archive and the `images` folder (the standalone installation
  package with archived images) to the user's home folder. The archive also contains the
  `docker-compose.yml` and `.env` configuration files (`.env` is hidden, so enable showing hidden
  files).

<FileTree
  tree={[
    {
      name: 'images',
      children: [
        { name: 'vadtel-nginx-latest.tar.gz' },
        { name: 'vadtel-app-latest.tar.gz' },
        { name: 'anytelemetry-latest.tar.gz' },
        { name: 'pgbouncer-latest.tar.gz' },
        { name: 'vadteljsondaemon-latest.tar.gz' },
        { name: 'logrotate-latest.tar.gz' },
        { name: 'postgres-15.1-alpine.tar.gz' },
        { name: 'redis-6.0.16-alpine.tar.gz' },
        { name: 'vector-latest.tar.gz' },
      ],
    },
  ]}
/>

- Copy the configuration files to `/opt/vadtelnewweb/`.

- Set the system logins and passwords in the `.env` file:

```bash title=".env"
#key
SECRET_KEY_BASE=0356080kcn0ejgk4lwekrf

#DB PGSQL login
PG_APP_USER=ppppp

#DB PGSQL password
PG_APP_PASSWORD= ppppp

#WEB interface login
VADTEL_SUPER_ADMIN_EMAIL=NEED_USERNAME

#WEB interface password
VADTEL_SUPER_ADMIN_PASSWORD=NEED_PASSWORD

#DB PGSQL unicloud login
VADTEL_UNICLOUD_TEST_USERNAME=NEED_USERNAME

#DB PGSQL unicloud password
VADTEL_UNICLOUD_TEST_PASSWORD=NEED_PASSWORD
```

### Install Docker and Docker Compose

#### On a CentOS-based distribution

Install Docker and Docker Compose:

```bash
sudo dnf install docker-ce docker-compose
```

Start Docker and enable it at boot:

```bash
sudo systemctl enable --now docker && systemctl start docker
```

#### On a Debian-based distribution

Install Docker and Docker Compose:

```bash
sudo apt update
sudo apt install docker docker-compose
```

### Install the system

The remaining steps are the same on every operating system. Add the user that will run the
application to the `docker` group:

```bash
sudo usermod -aG docker ${USER}
```

<Alert type="warning">
  After running this command, end the session and log in again to apply the new permissions.
</Alert>

Unpack the `unicloud.tgz` archive in the user's home folder:

```bash
tar -xf unicloud.tgz
```

<FileTree
  tree={[
    {
      name: 'unicloud',
      children: [
        {
          name: 'run',
          state: 'closed',
          children: [{ name: 'db-unicloud', type: 'folder', state: 'closed' }],
        },
        { name: 'remote_install.sh' },
        { name: 'init-db', children: [{ name: 'create_tables.sql' }] },
        { name: 'script_conf.sh' },
        { name: 'docker-compose.yml' },
        {
          name: 'logs',
          state: 'closed',
          children: [
            { name: 'crontab', type: 'folder', state: 'closed' },
            { name: 'vadteljsondaemon', type: 'folder', state: 'closed' },
            { name: 'anytelemetry', type: 'folder', state: 'closed' },
          ],
        },
        { name: 'backups', type: 'folder', state: 'closed' },
        {
          name: 'data',
          state: 'closed',
          children: [{ name: 'db-unicloud', type: 'folder', state: 'closed' }],
        },
        {
          name: 'local-images',
          children: [
            { name: 'pgbouncer.tar.gz' },
            { name: 'postgres.tar.gz' },
            { name: 'vadteljsondaemon.tar.gz' },
            { name: 'anytelemetry.tar.gz' },
            { name: 'logrotate.tar.gz' },
          ],
        },
        { name: 'unicloud.service' },
      ],
    },
  ]}
/>

Open the unpacked `unicloud` folder in the user's home folder and run the installation script:

```bash
sudo ./remote_install.sh
```

After the package installs, open `/var/lib/unicloud` with administrator privileges and change the
owner and group of the `.env` file to the current user.

The script generates the database login and password in `/var/lib/unicloud/.env`. Copy these values
into `/opt/vadtelnewweb/.env`:

```bash
VADTEL_UNICLOUD_TEST_USERNAME=NEED_USERNAME

VADTEL_UNICLOUD_TEST_PASSWORD=NEED_PASSWORD
```

### Extract and load the images

Unzip the image files in the previously copied `images` folder:

```bash
gunzip vadtel-nginx-latest.tar.gz
gunzip vadtel-app-latest.tar.gz
gunzip anytelemetry-latest.tar.gz
gunzip pgbouncer-latest.tar.gz
gunzip vadteljsondaemon-latest.tar.gz
gunzip logrotate-latest.tar.gz
gunzip postgres-15.1-alpine.tar.gz
gunzip redis-6.0.16-alpine.tar.gz
gunzip vector-latest.tar.gz
```

Load the images into Docker:

```bash
docker load -i vadtel-nginx-latest.tar
docker load -i vadtel-app-latest.tar
docker load -i anytelemetry-latest.tar
docker load -i pgbouncer-latest.tar
docker load -i vadteljsondaemon-latest.tar
docker load -i logrotate-latest.tar
docker load -i postgres-15.1-alpine.tar
docker load -i redis-6.0.16-alpine.tar
docker load -i vector-latest.tar.gz
```

Check that the images are available:

```bash
docker images
```

All nine images you loaded should appear in the list.

If the images loaded without errors, run the following command from the `/var/lib/unicloud` folder:

```bash
docker-compose up -d
```

Create a directory for the `vadtelnewweb` application under `/opt`:

```bash
sudo mkdir /opt/vadtelnewweb
cd /opt/vadtelnewweb
```

Then copy the `docker-compose.yml` and `.env` files (if you didn't copy them earlier for a new
installation) to `/opt/vadtelnewweb`.

On the first run, create the database:

```bash
docker-compose run app bundle exec rails db:create
```

Now start the application:

```bash
docker-compose up -d
```

<Details title="Troubleshooting">
If the application reports the following error after startup:

```bash
network unicloud_unicloud-net declared as external, but could not be found
```

the external network was not created during installation. Perform the initial installation from the
`unicloud.tar.tgz` archive as described in [Install the system](#install-the-system), then start the
application again:

```bash
docker-compose up -d
```

</Details>

Check the running containers:

```bash
docker ps
```

All containers should be listed with status `Up`.

### Check the system

After the application starts, open the IP address of the server it runs on in your browser to
confirm that the system is up:

```bash
http://127.0.0.1/auth/users/sign_in
```

<Image
  src="/images/deployment/sign-in-en.webp"
  alt="IIoT Platform sign-in page with email and password fields"
  caption="IIoT Platform sign-in page"
  width={1873}
  height={902}
/>

The sign-in email and password are set in `/opt/vadtelnewweb/.env`:

```shell title=".env"
VADTEL_SUPER_ADMIN_EMAIL= (login)
VADTEL_SUPER_ADMIN_PASSWORD= (password)
```

</Steps>

## Next steps

With the platform installed and running, set it up to update cleanly and to restart automatically with the server.

<Cards>
  <Card
    icon="refresh-cw"
    title="Updating and autostart"
    description="Update the platform to new image versions and make it start automatically with the server."
    href="/en/platform/v3/installation/updates"
  />
</Cards>