o
    AIdD                     @   s4  d dl Z d dlZd dlZd dlZd dlZe ZejZejZej	Z	ej
ZejZejZejZe jeejZdd ejD Zed g dZdd Zdd	 Zd
d Zdd Zdd Zd0ddZdd Z dd Z!dd Z"dd Z#dd Z$d1ddZ%d d! Z&d"d# Z'd$d% Z(d&d' Z)d(d) Z*d*d+ Z+d,d- Z,d.d/ Z-dS )2    Nc                 C   s2   g | ]}|d  dkr|d d tj|d  fqS )nameswap
mountpoint
/dev/{}/{})formatconfigvolgroup).0volume r   V/home/thorsten/Data/Arbeit/PerFact/Git/ema-modules/MigrationMonster/bin/create_disk.py
<listcomp>    s    

r   )z/boot/dev/nbd0p1)z/dev/z/dev/ptsz/procz/sysc                	   C   s<   t d  tjddddddttjgdd tjd	gdd d
S )zGenerate image fileqemu-imgcreatez-fqcow2-ozpreallocation=metadataTchecksyncN)print
subprocessrunimage_filenamer   
image_sizer   r   r   r   
make_image-   s   r   c                  C   s  t d d} td tjddddd| gd	d
 d}tjddd|ddddtjg	d	d
 d}tjddd|ddtj|gd	d
 tjddd|ddddgd	d
 tjddd|ddddgd	d
 t d tjddgd	d
 tjg dd	tjd t d tjg dd	d
 tjddtjdgd	d
 t d tj	D ]W}tjddd |d! d"|d# tjgd	d
 |d# d$krtjdd%d&
tj|d# gd	d
 tjdd'd(d$d&
tj|d# gd	d
 qtjdd)d&
tj|d# d(|d# gd	tjd qd*S )+z!Create partitions and filesystemszPartition diskmsdos   sudoparted-s	/dev/nbd0mklabelTr   mkpartprimaryext4z0%z100%set1booton2lvmzBoot file system	partprobe)r   	mkfs.ext4-Lr(   -m0r   )r   stdoutzMake LVM group)r   pvcreate/dev/nbd0p2vgcreater3   zCreate volumes and file systemslvcreatez--sizesizez--namer   r   mkswapr   	swaplabelr.   r-   N)r   timesleepr   r   r   boot_partition_sizeDEVNULLr   volumesr   )	labeltypetarget_disk	full_sizer
   r   r   r   make_filesystems7   s   




rA   c                   C   s:   t d  tjg ddd tjdddddd	tgdd d
S )zMount image into block device)r   modprobenbdTr   r   qemu-nbdz--connect=/dev/nbd0z--persistentz--forkz
--shared=4N)r   r   r   r   r   r   r   r   make_blockdev   s   
rE   c                  C   s>   t d g ddgddgg} | D ]}tjdg| dd qd	S )
zRemove loopback devicezRemove the loopback device)rD   z--disconnectr!   r,   rmmodrC   r   Fr   N)r   r   r   )cmdscmdr   r   r   destroy_blockdev   s   rI   c                  C   s~   t d  tddg tddddtjg tD ]$\} }tjt	| 
d}tjddd|gd	d
 tjdd||gd	d
 qdS )zMount file systemsr   r,   vgchange-ay/mkdir-pTr   mountN)r   r   r   r   r   mountpointsospathjoin
mount_pathlstrip)r   device	directoryr   r   r   mount_system   s    rY   Fc                 C   sb   t d ttD ]\}}tjdd|gtjdd q| rt  t d tjdddd	tjgdd
 dS )z=Reverse all setup procedures and return system back to normalzUnmount systemr   umountFstderrr   zRemove LVM volumesrJ   rK   nr   N)	r   reversedrQ   r   r   r<   zerofree_systemr   r   )zerofreer   rW   r   r   r   unmount_system   s   
ra   c                  C   s0   t d tD ]\} }tjddd|gdd qdS )zRun zerofree on all partitions.zZerofree file systemsr   r`   z-vFr   N)r   rQ   r   r   )r   rW   r   r   r   r_      s   r_   c               	   C   s   g d} t d t| dtdtjttjg  t d t d}tdd|g t| d|dt d	tj	 g  d
dd tj
D v rOtdddt dg dS dS )z5Unpack archive into freshly created and mounted image)tarz	--extractz--preserve-permissionsz--same-permissionsz--numeric-ownerzUnpack archivez--directoryz--filezUnpack database,/vol/postgresql/var/lib/postgresql/migrationrN   rO   rM   pg_workc                 S   s   h | ]}|d  qS )r   r   )r	   volr   r   r   	<setcomp>   s    z unpack_system.<locals>.<setcomp>lnr    z/vol/pg_workz*/vol/postgresql/var/lib/postgresql/pg_workN)r   _sudorU   rR   rS   rT   	base_pathr   template_archivetemplate_archive_dbr=   )rH   targetr   r   r   unpack_system   s$   

rm   c                   C   s   t d tdgddg dS )zInstall grubz&Install grub in the chroot environmentzupdate-grubzgrub-installr!   N)r   _chroot_cmdsr   r   r   r   install_grub   s
   ro   c                  C   s   t d tD ]} tjt| d}tjdddd| |gdd qtjdd	tjtd
tjtdgdd tjdddtjtd
gdd dS )z.Add mountpoints for chrooting into the system.zMount devices for chrootrM   r   rP   r   bindTr   mvetc/resolv.confetc/resolv.conf-chrootcpz/etc/resolv.confN)	r   chroot_mountpointsrR   rS   rT   rU   rV   r   r   r   rX   r   r   r   mount_chroot  s,   
rw   c                  C   sp   t d tjddtjtdtjtdgdd ttD ]} tjt| 	d}tjdd	|gtj
dd
 qdS )zRemove system mount pointszUnmount devices for chrootr   rq   rs   rr   Fr   rM   rZ   r[   N)r   r   r   rR   rS   rT   rU   r^   ru   rV   r<   rv   r   r   r   unmount_chroot  s    rx   mainc           	      C   s  	 d}t jddt d| dgt jdddj }d}| d| }| d|  }ztt d|  W n	 ty>   Y nw t	d	 d
S t	d t
dd|gd||gdd|||gdd|g | dkr| d}d| }| d}t
dd|gddd|gd||gdd||g |S )z3Move postgresql restore directory into proper placerc   r   catrM   z/PG_VERSIONT)r1   universal_newlinesr   z"/vol/postgresql/var/lib/postgresqlz'PostgreSQL directory found. Bailing outNzPerform postgresql fixuprN   rO   rq   chownzpostgres:postgreschmod0700ry   z/pg_walz/vol/pg_wal/z/maintest!z-drg   r    )r   r   rU   PIPEr1   striprR   statOSErrorr   rn   )	clustersource
pg_versionpg_homerl   pg_datapgwalpgwaldirpgwaltgtr   r   r   fixup_postgresql(  sL   




r   c                  C   sB   t d dD ]\} }d|}tddd|gdd| |g qd	S )
z<
    Create subfolders in /vol/backup. Requires chroot.
    zCreate folders in /vol/backup))postgres
postgresql)rootsystem)zoper   z/vol/backup/{}rN   r/   0770r|   z	{}:backupN)r   r   rn   )userfolderrl   r   r   r   create_backup_foldersU  s   

r   c                  C   sv   t tdd} | du rdS tj| std dS td t d}tjddd|gd	d
 tjddd| d|gd	d
 dS )z,
    Unpack source archive into chroot.
    source_archiveNzWarning: source_archive set but not present. Skipping extraction. Have you forgotten to run fetch-archive for the source system?zUnpacking source dataz+/opt/perfact/custom/migration/source_systemr   rN   rO   Tr   rb   xf-C)	getattrr   rR   rS   existsr   rU   r   r   )archiverl   r   r   r   unpack_source_dataf  s   

r   c                  C   s:  d} t ddd| gddd| g d}g }t d}t| D ] }| d	kr)d
}q| dkr7d}|d q|s>|| qd}tjdddt d| gd
d tjddt d| dgtjd
d	|d g }tt d D ]}|
drd| d}|| qrtjddt d| dgtjd
d	|d dS )a  
    1. Change permissions to Data.FS so user perfact can use
    zoperecord/zopeplayback.
    2. Create copy of Zope config file that accesses the Data.FS directly
    (without ZEO).
    3. Create copy of zodbsync config file that uses the file from 2.
    z /var/lib/zope2.13/zeo/emazeo/varchgrp-Rperfactr}   zg+wFz,/var/lib/zope2.13/instance/ema/etc/zope.confz<zeoclient>Tz</zeoclient>z
              <filestorage>
                path /var/lib/zope2.13/zeo/emazeo/var/Data.fs
              </filestorage>
            z!/opt/perfact/custom/migration/tmpr   rN   rO   rM   r   teez
/zope.conf )r1   textinputz/etc/perfact/modsync/zodb.pyzconf_path = zconf_path = 'z/zope.conf'
z
/zodb.confN)rn   rU   open	readlinesr   appendr   r   r<   rT   
startswith)zeovardroplineszopeconflinetmpdirr   r   r   _prepare_datafs_access|  sP   






r   c               
   C   s`   t   d} d}d}td|  d| d| dgd|  d| | gdd	d
dd|  ddg dS )z>Include Data.FS of source system in target under /PerFact/OLD.z/opt/perfact/custom/migrationz/opt/perfact/dbutils-zoperepoz)/var/lib/zope2.13/instance/ema/Extensionsrq   z/source_system/z	/__root__z/__root__/PerFact/OLDr   z-ur   zperfact-zopeplaybackz-cz/tmp/zodb.confz/PerFact/OLDN)r   rn   )	migrationrepo
extensionsr   r   r   upload_source_datafs  s"   r   c                  C   sl   t tdd} | du rdS tj| std dS td tddtjdt dg t	d	d
}t
d|g dS )z@
    Unpack the source db and set it up to serve port 5433.
    source_archive_dbNzWarning: source_archive_db set but not present. Skipping extraction. Have you forgotten to run fetch-archive for the source system?zUnpacking source databaserb   r   r   rc   r   )r   z8/opt/perfact/migration/src/inchroot/setup-source-cluster)r   r   rR   rS   r   r   rh   r   rU   r   rn   )r   datadirr   r   r   unpack_source_db  s    

r   c                  C   s*  t  } d}d}d}t | }tj|sJ dddtjg}dd|g}tj	|g d dd	
 }td
dd| d |  |g t|dd|g dd t|dd||g  t|ddg  t|ddg  t|dd|g  t|dd|d| g  t|d|g  t|dd|g  tddd|g dS ) zD
    Push the current state of this repository into the chroot.
    z/opt/perfact/migrationzbuild-targetmnttempz&Tool repository not found in template.gitr   )z	rev-parsez--abbrev-refHEADT)r{   r   r|   r   :remotermFr   addresetz--hardcleanz-dfcheckoutz-Bpushz--forcezHEAD:branchz-Dzperfact:perfactN)getpassgetuserrU   rR   rS   r   envri   r   check_outputr   _runrn   )current_userpath_in_chrootremote_name
tmp_branchrS   gitsrcgittgtcurrent_branchr   r   r   update_migration_tools  s8   


r   c                  C   s:   t tdd} | du rdS tjddd| t dgdd dS )	zH
    Clone the given custom file repository into the target system.
    custom_files_repoNr   r   clonez#/opt/perfact/custom/migration/filesTr   )r   r   r   r   rU   )r   r   r   r   clone_custom_files  s   
r   c                   C   sD   t jdu rdS td tjddddt jttjt	t j
gdd dS )	zConvert disk into VM formatNz Exporting image to target formatr   convertrO   z-OTr   )r   image_file_vm_formatr   r   r   r   rR   rS   rT   
build_pathimage_file_vmr   r   r   r   convert_disk  s   

r   )F)ry   ).rR   r9   r   r   helpers
create_envr   ri   r   rU   disk_configr   r   r   r   rh   chroot_cmdsrn   rS   rT   
image_filer   r=   rQ   r   ru   r   rA   rE   rI   rY   ra   r_   rm   ro   rw   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sN   


d

		
-<)