Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Deploying my Python programme on my Gentoo machine
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
Kiluanji
n00b
n00b


Joined: 19 Jan 2021
Posts: 26

PostPosted: Fri Apr 02, 2021 5:59 am    Post subject: Deploying my Python programme on my Gentoo machine Reply with quote

2 months ago, I successfully installed Gentoo on my Dell machine with a lot of help from this community.

Now I want to deploy a rudimentary Python programme on it, and have it initialise and execute itself automatically. I don't know where to begin. Would it be possible to point me in the right direction?

Thank you.
Back to top
View user's profile Send private message
mustafasalih1993
n00b
n00b


Joined: 09 Feb 2021
Posts: 38

PostPosted: Fri Apr 02, 2021 6:33 am    Post subject: Reply with quote

Hello Kiluanji

you can start by writing your own local ebuild with the help of this guide
https://wiki.gentoo.org/wiki/Basic_guide_to_write_Gentoo_Ebuilds

and read contribution guides
https://www.gentoo.org/get-involved/contribute/
Back to top
View user's profile Send private message
Goverp
Veteran
Veteran


Joined: 07 Mar 2007
Posts: 1996

PostPosted: Fri Apr 02, 2021 8:01 am    Post subject: Reply with quote

Wow, writing an ebuild's a bit heavy if Kiluanji's just writing some python code. OK, it depends on what's meant by "deploy a rudimentary Python programme". It may indeed be a case of adding something to the system, but it might just be running a user program from some other system or code repository, in which case the question is "how do I run a user-space python program on a Gentoo system".

I'm not a python programmer, so I had a look for relevant advice. I liked the title of Michał Górny's Gentoo Python Guide, but that appears to be answering a much more complex question, as is the Gentoo wiki article. I've not found something more directly useful, but that's down to my lack of Python skills.
_________________
Greybeard
Back to top
View user's profile Send private message
Kiluanji
n00b
n00b


Joined: 19 Jan 2021
Posts: 26

PostPosted: Fri Apr 02, 2021 8:48 am    Post subject: Reply with quote

Thank you both for your replies. Goverp seems to be closer to what I am considering: I envision uploading my user programme to GitHub, then have it fetched by my Gentoo machine and run automatically. You can probably tell by the ambiguity in my language that I am not at all proficient in systems administration.

I don't think my concern is Python-specific, and I suspect that at some point I will have to write a command line script for this.

Then again, perhaps an ebuild is what I need, I don't know. But the most fitting solution would be the simplest one for my purpose.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21602

PostPosted: Fri Apr 02, 2021 3:31 pm    Post subject: Reply with quote

Could you explain more precisely when/how this will work? Hypothetically, if you had to do all this by hand, at what points in the computer's operation would you do each of the steps? Does the script update only when the administrator chooses to, or should it be updated every time it runs? Does it run when the system first activates the network (before anyone logs in), when the user logs in (anywhere, including at Linux console), or when the user logs in to the GUI? If it is sensitive to user login, does it run for every user or just your personal user? Does it require root privilege? Does it require access to any particular user state, such as the ability to interact with the user's X desktop?
Back to top
View user's profile Send private message
Kiluanji
n00b
n00b


Joined: 19 Jan 2021
Posts: 26

PostPosted: Sat Apr 03, 2021 10:11 am    Post subject: Reply with quote

The operation should run with as little user input as possible.

I would login to manually download the programme, and it should then run itself periodically without any further input.

Updates should be the exception, and these are to be done manually.

The programme should run when the system first activates the network - at the times prescribed by the programme itself.

On the question of user privileges, since I am the only user of my Gentoo machine and its purpose will be only to run financial trading programmes, I didn't create any user, and there is only the root user there.

The programme should be completely standalone, therefore requiring no user state access.

Thank you for your help, Hu. If nothing else, the questions helped clarify in my mind what I am looking for.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21602

PostPosted: Sat Apr 03, 2021 4:43 pm    Post subject: Reply with quote

On principal, I would not run this program as root, because you have not described any reason it needs root privileges, and best practice is that programs only run as root if they need root privileges. For your use case, any unprivileged user with network (and possibly filesystem) access should be fine. I would create a dedicated unprivileged user and have it run as that user. Since you want it to be running all the time, you need it to be started by the init system, and preferably tell the init system that the program requires the network to be up. You would then have the program sleep when there is nothing to do, waiting for that to change. Which init system are you using: systemd, openrc, or something exotic?

Since install/update are handled outside the program, you could have an ebuild to do that.
Back to top
View user's profile Send private message
Ralphred
Guru
Guru


Joined: 31 Dec 2013
Posts: 495

PostPosted: Sun Apr 04, 2021 12:48 am    Post subject: Reply with quote

Hu: Can you have an ebuild automatically install a service in "current" runlevel, or would that be considered "not the way to do things"?

Kiluanji: I've written a number of daemons in python that are compatible with both openrc and systemd for automatic start/stop at system start-up and shutdown. If you would be interested in relevant code snippets to cut down on your dev time I can publish them for you as an easier starting point? I have code examples of /etc/init.d/daemon_name for openrc and similar daemon_name.service files for systemd too.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21602

PostPosted: Sun Apr 04, 2021 3:59 am    Post subject: Reply with quote

It is almost certainly possible via a pkg_postinst hook. I am unsure whether this would be considered a QA violation, but for a personal use ebuild, there is no one to complain about policy violations.
Back to top
View user's profile Send private message
Kiluanji
n00b
n00b


Joined: 19 Jan 2021
Posts: 26

PostPosted: Mon Apr 05, 2021 5:16 am    Post subject: Reply with quote

Hu wrote:
On principal, I would not run this program as root, because you have not described any reason it needs root privileges, and best practice is that programs only run as root if they need root privileges. For your use case, any unprivileged user with network (and possibly filesystem) access should be fine. I would create a dedicated unprivileged user and have it run as that user.


On it. Thanks.

Quote:
Since you want it to be running all the time, you need it to be started by the init system, and preferably tell the init system that the program requires the network to be up. You would then have the program sleep when there is nothing to do, waiting for that to change. Which init system are you using: systemd, openrc, or something exotic?

Since install/update are handled outside the program, you could have an ebuild to do that.


It seems I have the SysV init system - that's what I get when I run

Code:
# /sbin/init --version


And since I have seen systemd more frequently, I think I would like to make the jump to that as well

So ebuild vs wget from github.com... I suspect the latter is a simpler solution. Am I correct? If so, what would make ebuild the preferrable solution?

Ralphred wrote:
Kiluanji: I've written a number of daemons in python that are compatible with both openrc and systemd for automatic start/stop at system start-up and shutdown. If you would be interested in relevant code snippets to cut down on your dev time I can publish them for you as an easier starting point? I have code examples of /etc/init.d/daemon_name for openrc and similar daemon_name.service files for systemd too.


That would be great, yeah. I could first have a look at it and see how much of it I grasp. As you can tell, this is all a new world for me.

Thanks again for your help.
Back to top
View user's profile Send private message
Ralphred
Guru
Guru


Joined: 31 Dec 2013
Posts: 495

PostPosted: Mon Apr 05, 2021 7:37 am    Post subject: Reply with quote

Kiluanji wrote:
If so, what would make ebuild the preferrable solution?

An ebuild can do all prep work for you, creating a user (if you chose to do that), install the support scripts for automation (init scripts, .service files for systemd, if_up/down for tighter network integration if needed, even a cron job to email someone if an update is available etc)
It's a really teachable moment in the logic of "applied laziness", a little extra effort now (OK maybe more than a little) while the whole project is fresh in your head, the time you'll save in future deployments will be immeasurable. It's also a bonus that by combining the supporting configuration into your "source file(s)" for the ebuild, if you need future support from here, context is easily availed to those who would seek to help.
Kiluanji wrote:
That would be great, yeah. I could first have a look at it and see how much of it I grasp. As you can tell, this is all a new world for me.
https://liquid.me.uk/for_Kiluanji/
If there is some useful snippet you need an explanation on feel free to ask.

Also if you've got better practices for my initscripts/.service files chaps, I'm all ears.
Back to top
View user's profile Send private message
Kiluanji
n00b
n00b


Joined: 19 Jan 2021
Posts: 26

PostPosted: Tue Apr 06, 2021 5:21 am    Post subject: Reply with quote

Thanks a lot, Ralphred. Quite a bit to chew there. It will take me some time to get a grasp of it, but I will certainly go through it.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum