
    (tf5                        d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dl	Z	d dl
Z
d dlmZmZmZmZ  e j                           e j"                  dddde       e j"                  dd	d
de       e j"                  dddde       e j"                  dddde       e j"                  dddde       e j"                  dddde       e j"                  dddde       e j"                  ddd !       e j"                  d"dd#!       e j"                  d$dd%!       e j"                  d&dd'(       e j"                  d)d*d+e       e j"                  d,d-d.e       e j"                  d/d0d1e       e j"                  d2d3d4e       e j"                  d5d6d7e      d8                                                                                                                        Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d:d9Zy);    N)CONTAINER_PREFIXNETWORK_PREFIXREDIS_PREFIX
REDIS_CONFz-tz
--temp-dir/tmp/higlass-dockerzThe temp directory to use)defaulthelptypez-dz
--data-dir	~/hg-dataz!The higlass data directory to usez-vz	--versionlatestzeThe version of the Docker container to use (or "local" for images built locally using higlass-docker)z-pz--port#  z0The port that the HiGlass instance should run onz-nz	--hg-namer   z"The name for this higlass instancez-sz
--site-urlzXWhen creating an external-facing instance, enter its IP or hostname using this parameterz-mz--media-dirz2Use a specific media directory for uploaded files.z--public-data/--no-public-dataTz@Include or exclude public data in the list of available tilesets)r   r	   z--default-track-optionsz4Specify a json file containing default track optionsz	--workerszCSpecify a custom number of workers for the uWSGI application serverz--use-rediszHInitialize a Redis-based caching service and bind higlass instance to it)is_flagr	   z--redis-dir~/redis-dataz(Use a specific directory for Redis filesz--hg-repositoryhiglass/higlass-dockerz2The Docker repository to use for the HiGlass imagez--redis-repositoryredisz0The Docker repository to use for the Redis imagez--redis-tag5.0.3-alpinez4The Docker tag to use for the Redis image repositoryz--redis-port  z#The port to use for the Redis imagec                 8    t        | |||||||||	|
|||||       y )N)_start)temp_dirdata_dirversionporthg_namesite_url	media_dirpublic_datadefault_track_optionsworkers	use_redis	redis_dirhg_repositoryredis_repository	redis_tag
redis_ports                   ^/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/higlass_manage/start.pystartr'      s=    f !    c           
      0   dj                  t        |      }t        j                         }	 |j                  j                  |      }t        j                  j                  d       |j                          |j                          |
rdj                  t         |      }dj                  t"        |      }	 |j$                  j'                  |g      }|rJ|j$                  j                  |      }t        j                  j                  d       |j                          	 |j$                  j-                  |d	
      }	 |j                  j                  |      }t        j                  j                  d       |j                          |j                          t        j                  j                  dj                  ||             t        j                  j/                          |j0                  j3                  ||      }t        j                  j                  d       t        j                  j/                          t5        j6                  |      }t5        j8                  |      st;        j<                  |       t:        j>                  jA                  t:        j>                  jC                  t:        j>                  jE                  tF                    dd      }t:        j>                  j9                  |      sCt        j                  j                  dj                  |             t        j*                  d       |ddd|tH        ddi}dj                  tH              }	 |j                  jK                  |||||d      }|dk(  r|j0                  j                  d      }nt        j                  j                  d       t        j                  j/                          |j0                  j3                  ||      }t        j                  j                  d       t        j                  j/                          t5        j6                  |      }t5        j6                  |       } t5        j8                  |       st;        j<                  |        t5        j8                  |      st;        j<                  |       i }|||d <   |	|	|d!<   t        j                  j                  d"j                  |             t        j                  j                  d#j                  |              |d$nd%j                  |      }t        j                  j                  d&j                  ||             | d'dd|dddi} |rd(dd| |<   d(|d)<   |
s$|j                  jK                  |d*|i| ||d+      }n/|d,<   ||d-<   |j                  jK                  |d*|i| ||dd.      }t        j                  j                  d/j                  |             d0}!d1}"|!s	 t        j                  j                  d2j                  |"             |"d1z  }"t        j
                  d3j                  |      d45      }#|#jP                  d6k7  rNt        j                  j                  d7j                  |#jP                               tS        jT                  d8       nd}!|!st        j                  j                  d;j                  |             |r|6tY        jZ                         }$d<j                  |$      }%|j]                  |%      }&|st_        j`                  #jb                  je                  d=            }'d>g|'d?<   d}!d@|'dA}'|j]                  dB      }&t        jf                  dCj                  |      |'D      }&t        j                  j                  dEj                  |&jb                               |j]                  dFj                  $            }&t        j                  j                  dEj                  |&             |dti        |dG      5 }(t_        jj                  |(      })dHj                  t_        jl                  |)            }%|%dIz  }%|j]                  |%      }&ddd       dJ}%|%dKz  }%|j]                  |%      }&dL}%|%dKz  }%|j]                  |%      }&t        j                  j                  dM       t        j                  j                  dN       y# t        j                  j                  $ r Y t        j                  j                  $ r" t        j                  j                  d       Y yw xY w# t        j                  j(                  $ r8 t        j                  j                  d       t        j*                  d       Y w xY w# t        j                  j(                  $ rN}t        j                  j                  dj                  |             t        j*                  d       Y d}~d}~ww xY w# t        j                  j                  $ r Y t        j                  j                  $ r8 t        j                  j                  d       t        j*                  d       Y 	w xY w# t        j                  jL                  $ rN}t        j                  j                  dj                  |             t        j*                  d       Y d}~d}~wt        j                  jN                  $ rN}t        j                  j                  dj                  |             t        j*                  d       Y d}~d}~wt        j                  j(                  $ rN}t        j                  j                  dj                  |             t        j*                  d       Y d}~Rd}~ww xY w# t        j                  j                  $ r8 t        j                  j                  d9       tS        jT                  d8       Y lt        j                  jV                  $ r8 t        j                  j                  d:       tS        jT                  d8       Y w xY w# 1 sw Y   xY w)Oz"
    Start a HiGlass instance
    z{}-{}z&Stopping previously running container
zVError connecting to the Docker daemon, make sure it is started and you are logged in.
N)namesz6Attempting to remove existing Docker network instance
zHError: Could not access Docker network list to remove existing network.
bridge)driverz-Error: Could not access Docker network ({}).
z,Stopping previously running Redis container
z]Error: Error connecting to the Docker daemon, make sure it is started and you are logged in.
zPulling {}:{}
)tagzdone
r   z
redis.confz6Error: Could not locate Redis configuration file [{}]
z/datarw)bindmodezredis-server {}T)namenetworkvolumesdetachz/Error: Redis container could not be started
{}
z3Error: Redis container image could not be found
{}
z8Error: Redis container server ran into a fatal error
{}
localzimage-defaultzPulling latest image... 
SITE_URLWORKERSzData directory: {}
zTemp directory: ()
 z:{}zStarting... {} {}
z/tmpz/mediaHIGLASS_MEDIA_ROOTP   )portsr4   r2   environmentr5   
REDIS_HOST
REDIS_PORT)r3   r<   r4   r2   r=   publish_all_portsr5   zDocker started: {}
F   zsending request {}
z/http://localhost:{}/api/v1/viewconfs/?d=default   )timeout   z.Non 200 status code returned ({}), waiting...
g      ?zWaiting to start (tilesets)...
zRequest timed out
zpublic_data: {}
zYbash -c 'cp higlass-app/static/js/main.*.chunk.js higlass-app/static/js/main.{}.chunk.js'zutf-8z/api/v1trackSourceServersdefault_local)uidviewconfzpython higlass-server/manage.py shell --command="import tilesets.models as tm; o = tm.ViewConf.objects.get(uuid='default_local'); o.delete();" z%http://localhost:{}/api/v1/viewconfs/)jsonzret: {}
z^bash -c 'sed -i '"'"'s/"default"/"default_local"/g'"'"' higlass-app/static/js/main.*.chunk.js'rzobash -c 'sed -i '"'"'s/assign({{}},this.props.options/assign({{defaultOptions: {} }},this.props.options/g'"'"' z' higlass-app/static/js/main.*.chunk.js'zDbash -c 'sed -i '"'"'s/main.*.chunk.js/main.invalid.chunk.js/g'"'"' z$ higlass-app/precache-manifest.*.js'zMbash -c 'sed -i '"'"'s/index.html/index_invalidated_by_higlass_manage/g'"'"' zReplaced js file
zStarted
)7formatr   dockerfrom_env
containersgetsysstderrwritestopremoveerrorsNotFoundrequests
exceptionsConnectionErrorr   r   networkslistAPIErrorexitcreateflushimagespullop
expanduserexistsosmakedirspathjoindirnamerealpath__file__r   runContainerErrorImageNotFoundstatus_codetimesleep
ReadTimoutslugidniceexec_runrI   loadscontentdecodepostopenloaddumps)*r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   hg_container_nameclienthg_containernetwork_name
redis_namenetwork_listr3   errredis_containerredis_image
redis_confredis_volumesredis_commandhg_imagehg_environmenthg_version_addition
hg_volumesstartedcounterreqnew_hashsed_commandretconfigfdefault_options_jsons*                                             r&   r   r      s
   *  '7A__F((,,->?

BC ~~ng>^^L':
	!??//|n/EL //--l;

  M  	oo,,\(,KG	$//33J?OJJLM  """$ 	

*112BINO

mm(()9y(I

"

 MM),	yy#KK	"WW\\GGOOBGG,,X67,

 ww~~j)JJIPP
 HHRL 6T:

 *00<	$//33$% 4 O0 '==$$_5

56

==%%mW=

"

}}X&H}}X&H99X
H99X
HN%-z"$+y!JJ+228<=JJ+228<= '"U\\'5JJJ*11'4@A6407D1J
 )14 @
9/7+,((,,t*"& - 
 (2|$'1|$((,, t*"&" - 	
 JJ+223DEFGG	JJ3::7CDqLG,,AHHNC #%

  ELL
 

3% 4 JJ(//<=/; ;;=u||
 ##K0CKK..w78(1{#$(f=## b
 mm3::4@v
 	

++CKK89##pww

 	

++C01('- 		5#'99Q<  P  W  W

/0K DDK ''4C		5 	S  99K



,C fK99K



,CJJ)*JJ[!W ==!! .. 

e	
 		& }}%% 	JJ[ HHRL		 }}%% 	JJ@GGL HHRLL		 }}%% 	""22 	JJp HHRL		^ }}++ 	JJCJJ3O HHRLL}}** 	JJGNNsS HHRLL}}%% 	JJLSSTWX HHRLL		| ""22 	JJ?@JJsO""-- 	JJ23JJsO	P		5 		5s   Ad 6A)e#  f; >Ah) %!j  B8o  ;Are ";e e #Af87f8;h&Ah!!h&)j	Ajj o=Al o&Am// oAoo Ar4Arrr)r   r   r   r   r   NNTNNFr   r   r   r   r   )clickrL   rI   re   os.pathrg   rb   rW   rs   rP   rp   higlass_manage.commonr   r   r   r   commandoptionstrintr'   r    r(   r&   <module>r      s      	    
   !	$	 	,	 	p	 	;	 	-	 	c	 	=	 $	K
 	?
 	N
 	S
 	3		 $	=		 	;		 	?		 D'LSV# B#N #	*!j"r(   