summaryrefslogtreecommitdiffstats
path: root/linhes/linhes-system/lh_setup_fileshare.py
blob: 81eb5fa5f9ebd9c8eafee6acee6c0b4c48d179f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python

import glob, sys, subprocess, os, re, socket

def setup_samba():
    mythhome = subprocess.check_output("lh_home_check.sh").decode('utf-8').strip()
    excludes = ['mysql','srv']
    print("    Activating samba file sharing")
    usersamba=mythhome+"/templates/smb.conf"
    subprocess.call(["pacman", "-S", "--noconfirm", "samba", "avahi"])
    if not os.path.exists("/etc/samba"):
        print("    Creating directory /etc/samba")
        try:
            os.makedirs("/etc/samba")
        except:
            pass

    if os.path.exists(usersamba):
        print("    Using user provided config file " + usersamba)
        subprocess.call(["install", "-Dm755",  usersamba, "/etc/samba/smb.conf"])
    else:
        #Samba_media = systemconfig.get("Samba_media")
        #Samba_home = systemconfig.get("Samba_home")
        smreadonly = "yes"
        shreadonly = "yes"
        domain = "WORKGROUP"
        servername = socket.gethostname()

        try:
            f = open("/usr/share/linhes/templates/smb.conf.template",'r')
            t_smbconf = f.readlines()
            f.close()
        except:
            print("   Couldn't open samba template file")
            return

        try:
            f = open("/etc/samba/smb.conf",'w')
        except:
            print("   Couldn't open samba file")
            return

        for line in t_smbconf:
            outline = line
            if re.match("^.*workgroup", line):
                print("    Setting workgroup to " + domain)
                outline="   workgroup = %s\n" %domain
                print("     " + outline)
            if re.match("^.* server string",line):
                print("    Setting server name to " + servername)
                outline="   server string = %s\n" %servername
                print("     " + outline)
            f.write(outline)

        outline="include =  %s/templates/user.shares \n" %mythhome
        f.write(outline)
        outline="include =  /etc/samba/smb.conf.media\n"
        f.write(outline)
        outline="include =  /etc/samba/smb.conf.home\n"
        f.write(outline)
        f.close()

        print("    Writing smb.conf.media")
        try:
            f = open("/etc/samba/smb.conf.media","w")
        except:
            print("*   Couldn't open smb.conf.media")
            return

        shares = scan_for_shares()
        medialines='''
[%s]
path = %s
public = yes
only guest = yes
writeable  = %s
printable = no
force user = mythtv
force group = mythtv
create mask = 0755\n'''
        new_share=[]
        excludes
        for share in shares:
            share_name = share.split("/")[-1]
            share_path = share
            f.write(medialines %(share_name,share_path,smreadonly) )
            print(medialines %(share_name,share_path,smreadonly) )
            excludeline = 'veto files = '
            for exclude in excludes:
                excludeline+= ''' /%s/ ''' %exclude

            if excludes != []:
                f.write( excludeline)
                f.write("\n")

        f.close()
        print("    Writing smb.conf.home")
        try:
                f = open("/etc/samba/smb.conf.home","w")
        except:
            print("    Couldn't open smb.conf.home")
            return
        homelines='''
[home]
path = %s
public = yes
only guest = yes
writeable  = %s
printable = no
force user = mythtv
force group = mythtv
create mask = 0755 ''' %(mythhome,shreadonly)
        f.write(homelines)
        f.close()
        print("     " + homelines)

    print("\n    Creating samba user mythtv")
    os.system("(echo mythtv; echo mythtv) | smbpasswd -sa mythtv")
    print("\n    Starting SMB, NMB and avahi services")
    subprocess.call(["systemctl", "enable", "--now", "smb.service"])
    subprocess.call(["systemctl", "enable", "--now", "nmb.service"])
    subprocess.call(["systemctl", "enable", "--now", "avahi-daemon.service"])
    print("\n    Finished setting up samba file sharing")


def scan_for_shares():
    import configparser
    config = configparser.RawConfigParser()
    file_list=glob.glob("/etc/storage.d/*.conf")
    share_list = ['/data/storage/disk0']
    for conf_file in file_list:
        try:
            print("    reading in %s" %conf_file)
            config.read(conf_file)
            shareable = config.get('storage','shareable')
            if shareable == "True" :
                mp = config.get('storage','mountpoint')
                share_list.append(mp)
#                if config.get('storage','mmount') == "True" :
#                    share_list.append("/myth")
        except:
            print("    Couldn't open %s for reading" %conf_file)
    return share_list


def setup_fileshare():
    setup_samba()

if __name__ == "__main__":
    setup_fileshare()