pm2 creates a "source" directory and copies all my files inside, why?
Asked Answered
I

1

10

I deleted my previous question because it was not very clear, and the problem was not clearly exposed. I have an instance @aws, a repository @gitlab, and gitlab CI is setup. I made a little app in node.js because I want to try all these new stuff. But, when gitlab-ci runs the script, pm2 creates a "source" directory in my folder, then copied all my files in this directory, which is appearently the Current Working Directory (CWD). That's a surprising behavior, and I'm not comfortable with it.

Anyone knows why ? Is it normal ? Why can't my files stay in ~/projet2/, as I set up ?

When I run pm2 show projet2, I can see the exec cwd is /home/ubuntu/projet2/source while source is a folder I've never created !

.git-ci.yml

# This file is a template, and might need editing before it works on your project.
# Official framework image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/node/tags/
image: node:alpine

stages:
  - deploy

deploy:
  stage: deploy
  before_script:
    # Install ssh-agent if not already installed, it is required by Docker.
    # (change apt-get to yum if you use a CentOS-based image)
    - 'which ssh-agent || ( apk add --update openssh )'

    # Add bash
    - apk add --update bash

    # Add git
    - apk add --update git

    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - echo "$SSH_PRIVATE_KEY" > "./pk.pem"
    - chmod 400 ./pk.pem
    - echo "$SSH_PRIVATE_KEY" | ssh-add -

    # For Docker builds disable host key checking. Be aware that by adding that
    # you are suspectible to man-in-the-middle attacks.
    # WARNING: Use this only with the Docker executor, if you use it with shell
    # you will overwrite your user's SSH config.
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    # In order to properly check the server's host key, assuming you created the
    # SSH_SERVER_HOSTKEYS variable previously, uncomment the following two lines
    # instead.
    # - mkdir -p ~/.ssh
    # - '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'
  script:
  - npm i -g pm2
  - pm2 deploy ecosystem.config.js production setup
  - pm2 deploy ecosystem.config.js production
  only:
  - master

ecosystem.config.js

module.exports = {
  apps: [{
    name: 'projet2',
    script: '/home/ubuntu/projet2/index.js',
    cwd: '/home/ubuntu/projet2/'
  }],
  deploy: {
    production: {
      user: 'ubuntu',
      host: 'xxxxxxxxxxxx',
      ref: 'origin/master',
      repo: '[email protected]:xxxxxxx/projet2.git',
      key: './pk.pem',
      path: '/home/ubuntu/projet2/',
      'post-deploy': 'npm install && pm2 startOrRestart /home/ubuntu/projet2/ecosystem.config.js'
    }
  }
}
Incomparable answered 28/9, 2019 at 22:46 Comment(1)
I know this is older but in case others still end up here I wanted to say me too :) Also: My workaround is a hack to cp -r from /source to ./ as the first command in 'post-deploy'. I was unable to find the correct solution and this worked for my needs.Maxama
S
4

The answer is: Yes! This is normal behavior!

It is to be expected, since you are running things with pm2 now, and pm2 knows how to handle it.

By running:

pm2 deploy ecosystem.config.js someName

the pm2 is making an SSH to the provided host, using the provided user and key. Then, on a successful connection to the provided host, pm2 proceeds to try and do a git pull from the provided referenced branch inside ref, which belongs to the provided repo. The pulled data will be placed in the provided path inside 'path', with the addition of a 'source' directory. After a successful pull, the post-deploy will be triggered, which is in charge of doing the npm install and then some more stuff (depending on what you tell it to do). But nevertheless, the creation of the source folder is something that is built-in to the pm2 mechanism, and is to be expected. It shouldn't bother you too much.

Seduce answered 14/9, 2021 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.