[PYTHON] A story about operating a GCP instance from Discord

Introduction

This article is a continuation of A story about a GCP beginner building a Minecraft server on GCE. If you haven't seen the last time, I would appreciate it if you could see it from there.

Well, since I built a Minecraft server with GCP last time, This time I would like to be able to operate it from discord.

Is there any merit to operate the instance from discord? I think, Ask someone playing Minecraft to join the discord channel Since it can be started and stopped on behalf of the administrator, the burden on the administrator can be reduced.

Target

--Creating a server for bot --Create a service account for instance control --Create Discord Bot

procedure

1. Creating a server for bots

The server used for the bot this time uses Google Compute Engine (GCE). GCE has Always Free resources that are completely free to use, and this time we'll use them. Since it is always free, you will not be charged even if it is always running, and this time there is no use other than bot, so this resource is sufficient for operation.

For more information on GCE's Always Free, please refer to the official documentation below.

Google Cloud Free Tier

The instance was created with such settings. Detailed settings will be omitted this time.

item Contents
Instance name botserver
region us-west1
zone us-west1-a
Machine type f1-micro(vCPUx1,Memory 0.6GB)
Boot disk CentOS 7
IP address Create a static IP address

2. Create a service account for instance control

You can create a service account in GCP with only certain permissions to access projects and resources. For more information on service accounts, please refer to the official documentation below.

About Service Accounts (https://cloud.google.com/iam/docs/understanding-service-accounts?hl=ja#managing_service_accounts)

This time, give this service account permission to start and stop the instance (Minecraft server). First, create a "role" from "IAM and management".

Select IAM & Administration-> Roles-> Create Role and set the following:

item Contents
title Arbitrary title name
Authority compute.instances.get
compute.instances.start
compute.instances.stop

** [Description of authority] **

item Description
compute.instances.get Permissions to log in to your Compute Engine instance
compute.instances.start Permission to launch a Compute Engine instance
compute.instances.stop Permission to stop a Compute Engine instance

At this point, the creation of the "role" is complete. Next, create a "Service Account" from "IAM and Administration".

Select IAM & Administration-> Service Accounts-> Create Service Account and configure as follows:

item Contents
Service account name Any service account name
Service account ID Any service account ID@{ProjectID}.iam.gserviceaccount.com
Service account description Write a description to make it easier for you to understand
Role selection The "role" created earlier
Creating a key JSON type
*Click Create and download the json file.

Please do not lose the json file downloaded when creating the key, as you will need it later.

3. Create Discord Bot

3.1 Creating a bot account

First, create a developer account for your bot at the Discord Developer Portal.

I referred to the following articles for various settings from the creation of a developer account.

Discord Bot Account Initialization Guide for Developer

3.2 bot server settings

Log in to the instance created in 1. and make various settings.

First, install the required packages.


#CentOS7 does not have python3 installed by default, so install python3
$ yum install python3

# discord.py installation
$ python3 -m pip install -U discord.py

Next, authenticate the service account you created earlier.


#Upload the key file (json file) downloaded when creating the service account to the bot server, and authenticate with the following command.
$ gcloud auth activate-service-account --key-file "./<projectName>-xxxxxx.json"

#Check if the service account is active.*OK if your account is active
$ gcloud auth list

#Execution example below
---------------------------------------------------------------
ACTIVE  ACCOUNT
*       <Service account name>@<Project name>.iam.gserviceaccount.com
---------------------------------------------------------------

Creating a bot program

mineserver-op.py



#Installed discord.Load py
import discord
import os
import time

#Your bot's access token
TOKEN = '<Access token>'

client = discord.Client()

#Processing that operates at startup
@client.event
async def on_ready():
    #When started, a login notification will be displayed in the terminal
    print('Yaho! I logged in!')
    print('/You can check the command with help')

#Processing that operates when receiving a message
@client.event
async def on_message(message):
    #Do not process if the message sender is a bot
    if message.author.bot:
        return
#Start the server
    if message.content == '/start':
        await message.channel.send('Server starting up...')
        await message.channel.send('* Do not execute other commands until "start up" is displayed. *')
        os.system('gcloud --account=<Service account name>@<Project name>.iam.gserviceaccount.com compute instances start <Instance name> --project <Project name> --zone <Zone name>')
        await message.channel.send('up .minecraft_server starting...')
        time.sleep(60)
        await message.channel.send('start up')
#Server outage
    if message.content == '/stop':
        await message.channel.send('Server is stopping')
        await message.channel.send('* Do not execute other commands until "down" is displayed. *')
        os.system('gcloud --account=<Service account name>@<Project name>.iam.gserviceaccount.com compute instances stop <Instance name> --project <Project name> --zone <Zone name>')
        await message.channel.send('down')

#View help
    if message.content == '/help':
        await message.channel.send('/start :Start the server')
        await message.channel.send('/stop :Stop the server') 

client.run(TOKEN)

Execution of the created bot program

#Make sure to keep the execution log and execute
$ nohup python3 mineserver-op.py > ./out.log &

At this point, you can operate the instance from discord. Let's check if you can actually operate the instance by typing a command from discord.

Summary

This time, we have implemented only the minimum functions for starting and stopping instances, It is possible to implement various functions by giving the desired authority to the service account.

As an aside, I played Minecraft a little in this operation check. Since I was playing, there have been a lot of new elements, and I wanted to do Minecraft again (laughs).

Recommended Posts

A story about operating a GCP instance from Discord
The story of launching a Minecraft server from Discord
A story about reflecting Discord activity in Slack Status
A story about creating a UNIX / Linux compatible OS from scratch
A story about a GCP beginner building a Minecraft server on GCE
A refreshing story about Python's Slice
A sloppy story about Python's Slice
A story about using Python's reduce
A story about switching a personally developed Web service from a rental server to GCP (Google Cloud Platform)
A story about a beginner making a VTuber notification bot from scratch in Python
A story about machine learning with Kyasuket
A story about creating an anonymous channel on Slack from zero knowledge
A story about Python pop and append
A story about a 503 error on Heroku open
A story about trying to install uwsgi on an EC2 instance and failing
Escape from Python's virtual environment ~ A story about being trapped in a virtual environment I created ~
A story about simple machine learning using TensorFlow
A story about Go's global variables and scope
A story about displaying article-linked ads on Jubatus
A story about implementing a login screen with django
A story about running Python on PHP on Heroku
A story of creating 16 * 16 dots from a Digimon photo
A story about modifying Python and adding functions
A story about data analysis by machine learning
A story about making 3D space recognition with Python
A story about using Resona's software token with 1Password
A story about predicting exchange rates with Deep Learning
A story about migrating entire Linux disk via SSH
A story about making Hanon-like sheet music with Python
A story about trying a (Golang +) Python monorepo with Bazel
A story about kindergartens, nursery schools, and children's gardens
A story about struggling to loop 3 million ID data
A story about how theano was moved with TSUBAME 2.0
A story about changing the master name of BlueZ
A story about a Linux beginner passing LPIC101 in a week
A story about a Linux beginner putting Linux on a Windows tablet
A story about stumbling through PATH after installing anaconda